Home | History | Annotate | Download | only in auxprogs
      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-2011 OpenWorks LLP
     11       info (at) 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_s390x.h"
     55 
     56 #define VG_STRINGIFZ(__str)  #__str
     57 #define VG_STRINGIFY(__str)  VG_STRINGIFZ(__str)
     58 
     59 #define my_offsetof(__type,__field) (&((__type*)0)->__field)
     60 
     61 /* This forces gcc to evaluate the my_offsetof call at compile time,
     62    and then emits it in the assembly, along with the nonsense string
     63    "xyzzy", for easy greppability.  Once this file is compiled to
     64    assembly, the lines containing "xyzzy" are grepped out and sed-ed
     65    to produce the final result.  See the Makefile rule for
     66    pub/libvex_guest_offsets.h. */
     67 #define GENOFFSET(_structUppercase,_structLowercase,_fieldname)  \
     68    __asm__ __volatile__ ( \
     69       "\n#define OFFSET_" \
     70       VG_STRINGIFY(_structLowercase) "_" \
     71       VG_STRINGIFY(_fieldname) \
     72       " xyzzy%0\n" : /*out*/ \
     73                    : /*in*/ "n" \
     74          (my_offsetof(VexGuest##_structUppercase##State, \
     75           guest_##_fieldname)) \
     76    )
     77 
     78 void foo ( void );
     79 __attribute__((noinline))
     80 void foo ( void )
     81 {
     82    // x86
     83    GENOFFSET(X86,x86,EAX);
     84    GENOFFSET(X86,x86,EBX);
     85    GENOFFSET(X86,x86,ECX);
     86    GENOFFSET(X86,x86,EDX);
     87    GENOFFSET(X86,x86,ESI);
     88    GENOFFSET(X86,x86,EDI);
     89    GENOFFSET(X86,x86,EBP);
     90    GENOFFSET(X86,x86,ESP);
     91    GENOFFSET(X86,x86,EIP);
     92    GENOFFSET(X86,x86,CS);
     93    GENOFFSET(X86,x86,DS);
     94    GENOFFSET(X86,x86,ES);
     95    GENOFFSET(X86,x86,FS);
     96    GENOFFSET(X86,x86,GS);
     97    GENOFFSET(X86,x86,SS);
     98 
     99    // amd64
    100    GENOFFSET(AMD64,amd64,RAX);
    101    GENOFFSET(AMD64,amd64,RBX);
    102    GENOFFSET(AMD64,amd64,RCX);
    103    GENOFFSET(AMD64,amd64,RDX);
    104    GENOFFSET(AMD64,amd64,RSI);
    105    GENOFFSET(AMD64,amd64,RDI);
    106    GENOFFSET(AMD64,amd64,RSP);
    107    GENOFFSET(AMD64,amd64,RBP);
    108    GENOFFSET(AMD64,amd64,R8);
    109    GENOFFSET(AMD64,amd64,R9);
    110    GENOFFSET(AMD64,amd64,R10);
    111    GENOFFSET(AMD64,amd64,R11);
    112    GENOFFSET(AMD64,amd64,R12);
    113    GENOFFSET(AMD64,amd64,R13);
    114    GENOFFSET(AMD64,amd64,R14);
    115    GENOFFSET(AMD64,amd64,R15);
    116    GENOFFSET(AMD64,amd64,RIP);
    117 
    118    // ppc32
    119    GENOFFSET(PPC32,ppc32,GPR0);
    120    GENOFFSET(PPC32,ppc32,GPR1);
    121    GENOFFSET(PPC32,ppc32,GPR2);
    122    GENOFFSET(PPC32,ppc32,GPR3);
    123    GENOFFSET(PPC32,ppc32,GPR4);
    124    GENOFFSET(PPC32,ppc32,GPR5);
    125    GENOFFSET(PPC32,ppc32,GPR6);
    126    GENOFFSET(PPC32,ppc32,GPR7);
    127    GENOFFSET(PPC32,ppc32,GPR8);
    128    GENOFFSET(PPC32,ppc32,GPR9);
    129    GENOFFSET(PPC32,ppc32,GPR10);
    130    GENOFFSET(PPC32,ppc32,CIA);
    131    GENOFFSET(PPC32,ppc32,CR0_0);
    132 
    133    // ppc64
    134    GENOFFSET(PPC64,ppc64,GPR0);
    135    GENOFFSET(PPC64,ppc64,GPR1);
    136    GENOFFSET(PPC64,ppc64,GPR2);
    137    GENOFFSET(PPC64,ppc64,GPR3);
    138    GENOFFSET(PPC64,ppc64,GPR4);
    139    GENOFFSET(PPC64,ppc64,GPR5);
    140    GENOFFSET(PPC64,ppc64,GPR6);
    141    GENOFFSET(PPC64,ppc64,GPR7);
    142    GENOFFSET(PPC64,ppc64,GPR8);
    143    GENOFFSET(PPC64,ppc64,GPR9);
    144    GENOFFSET(PPC64,ppc64,GPR10);
    145    GENOFFSET(PPC64,ppc64,CIA);
    146    GENOFFSET(PPC64,ppc64,CR0_0);
    147 
    148    // arm
    149    GENOFFSET(ARM,arm,R0);
    150    GENOFFSET(ARM,arm,R1);
    151    GENOFFSET(ARM,arm,R2);
    152    GENOFFSET(ARM,arm,R3);
    153    GENOFFSET(ARM,arm,R4);
    154    GENOFFSET(ARM,arm,R5);
    155    GENOFFSET(ARM,arm,R7);
    156    GENOFFSET(ARM,arm,R13);
    157    GENOFFSET(ARM,arm,R14);
    158    GENOFFSET(ARM,arm,R15T);
    159 
    160    // s390x
    161    GENOFFSET(S390X,s390x,r2);
    162    GENOFFSET(S390X,s390x,r3);
    163    GENOFFSET(S390X,s390x,r4);
    164    GENOFFSET(S390X,s390x,r5);
    165    GENOFFSET(S390X,s390x,r6);
    166    GENOFFSET(S390X,s390x,r7);
    167    GENOFFSET(S390X,s390x,r15);
    168    GENOFFSET(S390X,s390x,IA);
    169    GENOFFSET(S390X,s390x,SYSNO);
    170    GENOFFSET(S390X,s390x,IP_AT_SYSCALL);
    171    GENOFFSET(S390X,s390x,fpc);
    172 }
    173 
    174 /*--------------------------------------------------------------------*/
    175 /*--- end                                             genoffsets.c ---*/
    176 /*--------------------------------------------------------------------*/
    177