1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/xpcom/reflect/xptcall/src/md/unix/vtable_layout_x86.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,66 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +/* this code contributed by Bert Driehuis <bert_driehuis@nl.compuware.com> */ 1.9 + 1.10 +#include <stdio.h> 1.11 + 1.12 +// Try to determine the vtable layout generated by G++ 1.13 +// Produces the offset at which the first vtable entry can be 1.14 +// found, and the factor to apply for subsequent entries on stdout. 1.15 +// Example output: 1.16 +// #define GCC_VTABLE_START 0xc 1.17 +// #define GCC_VTABLE_FACTOR 0x8 1.18 + 1.19 +class test { 1.20 +public: 1.21 + virtual int t1(void); 1.22 + virtual int t2(void); 1.23 + int x; 1.24 +}; 1.25 + 1.26 +test::test() { this->x = 0x12121212; }; 1.27 + 1.28 +int test::t1(void) { return 1; } 1.29 +int test::t2(void) { return 2; } 1.30 + 1.31 +void die(char *x) { 1.32 + fprintf(stderr, "%s\n", x); 1.33 + exit(1); 1.34 +} 1.35 + 1.36 +int 1.37 +main() 1.38 +{ 1.39 + int i; 1.40 + test *t = new test(); 1.41 + int *tp = (int *) t; 1.42 + int off1 = -1; 1.43 + int off2 = -1; 1.44 + int factor; 1.45 + int factorshift; 1.46 + 1.47 + if (*tp++ != 0x12121212) 1.48 + die("Integer element test::x not found!"); 1.49 + tp = (int *) *tp; 1.50 + for (i = 0; i < 10; i++) { 1.51 + if (tp[i] == (int) t->t1) 1.52 + off1 = i; 1.53 + if (tp[i] == (int) t->t2) 1.54 + off2 = i; 1.55 + } 1.56 + if (off1 == -1 || off2 == -1) 1.57 + die("Could not determine offset into vtable!"); 1.58 + factor = (off2 - off1) * 4; 1.59 + factorshift = -1; 1.60 + while (factor) { 1.61 + factorshift++; 1.62 + factor >>= 1; 1.63 + } 1.64 + printf("/* Automatically generated by vtable_layout_x86.cpp */\n"); 1.65 + printf("#define GCC_VTABLE_START\t0x%x\n", off1 * 4); 1.66 + printf("#define GCC_VTABLE_FACTOR\t0x%x\n", (off2 - off1) * 4); 1.67 + printf("#define GCC_VTABLE_SHIFT\t0x%x\n", factorshift); 1.68 + exit(0); 1.69 +}