michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /* this code contributed by Bert Driehuis */ michael@0: michael@0: #include michael@0: michael@0: // Try to determine the vtable layout generated by G++ michael@0: // Produces the offset at which the first vtable entry can be michael@0: // found, and the factor to apply for subsequent entries on stdout. michael@0: // Example output: michael@0: // #define GCC_VTABLE_START 0xc michael@0: // #define GCC_VTABLE_FACTOR 0x8 michael@0: michael@0: class test { michael@0: public: michael@0: virtual int t1(void); michael@0: virtual int t2(void); michael@0: int x; michael@0: }; michael@0: michael@0: test::test() { this->x = 0x12121212; }; michael@0: michael@0: int test::t1(void) { return 1; } michael@0: int test::t2(void) { return 2; } michael@0: michael@0: void die(char *x) { michael@0: fprintf(stderr, "%s\n", x); michael@0: exit(1); michael@0: } michael@0: michael@0: int michael@0: main() michael@0: { michael@0: int i; michael@0: test *t = new test(); michael@0: int *tp = (int *) t; michael@0: int off1 = -1; michael@0: int off2 = -1; michael@0: int factor; michael@0: int factorshift; michael@0: michael@0: if (*tp++ != 0x12121212) michael@0: die("Integer element test::x not found!"); michael@0: tp = (int *) *tp; michael@0: for (i = 0; i < 10; i++) { michael@0: if (tp[i] == (int) t->t1) michael@0: off1 = i; michael@0: if (tp[i] == (int) t->t2) michael@0: off2 = i; michael@0: } michael@0: if (off1 == -1 || off2 == -1) michael@0: die("Could not determine offset into vtable!"); michael@0: factor = (off2 - off1) * 4; michael@0: factorshift = -1; michael@0: while (factor) { michael@0: factorshift++; michael@0: factor >>= 1; michael@0: } michael@0: printf("/* Automatically generated by vtable_layout_x86.cpp */\n"); michael@0: printf("#define GCC_VTABLE_START\t0x%x\n", off1 * 4); michael@0: printf("#define GCC_VTABLE_FACTOR\t0x%x\n", (off2 - off1) * 4); michael@0: printf("#define GCC_VTABLE_SHIFT\t0x%x\n", factorshift); michael@0: exit(0); michael@0: }