Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | /* this code contributed by Bert Driehuis <bert_driehuis@nl.compuware.com> */ |
michael@0 | 6 | |
michael@0 | 7 | #include <stdio.h> |
michael@0 | 8 | |
michael@0 | 9 | // Try to determine the vtable layout generated by G++ |
michael@0 | 10 | // Produces the offset at which the first vtable entry can be |
michael@0 | 11 | // found, and the factor to apply for subsequent entries on stdout. |
michael@0 | 12 | // Example output: |
michael@0 | 13 | // #define GCC_VTABLE_START 0xc |
michael@0 | 14 | // #define GCC_VTABLE_FACTOR 0x8 |
michael@0 | 15 | |
michael@0 | 16 | class test { |
michael@0 | 17 | public: |
michael@0 | 18 | virtual int t1(void); |
michael@0 | 19 | virtual int t2(void); |
michael@0 | 20 | int x; |
michael@0 | 21 | }; |
michael@0 | 22 | |
michael@0 | 23 | test::test() { this->x = 0x12121212; }; |
michael@0 | 24 | |
michael@0 | 25 | int test::t1(void) { return 1; } |
michael@0 | 26 | int test::t2(void) { return 2; } |
michael@0 | 27 | |
michael@0 | 28 | void die(char *x) { |
michael@0 | 29 | fprintf(stderr, "%s\n", x); |
michael@0 | 30 | exit(1); |
michael@0 | 31 | } |
michael@0 | 32 | |
michael@0 | 33 | int |
michael@0 | 34 | main() |
michael@0 | 35 | { |
michael@0 | 36 | int i; |
michael@0 | 37 | test *t = new test(); |
michael@0 | 38 | int *tp = (int *) t; |
michael@0 | 39 | int off1 = -1; |
michael@0 | 40 | int off2 = -1; |
michael@0 | 41 | int factor; |
michael@0 | 42 | int factorshift; |
michael@0 | 43 | |
michael@0 | 44 | if (*tp++ != 0x12121212) |
michael@0 | 45 | die("Integer element test::x not found!"); |
michael@0 | 46 | tp = (int *) *tp; |
michael@0 | 47 | for (i = 0; i < 10; i++) { |
michael@0 | 48 | if (tp[i] == (int) t->t1) |
michael@0 | 49 | off1 = i; |
michael@0 | 50 | if (tp[i] == (int) t->t2) |
michael@0 | 51 | off2 = i; |
michael@0 | 52 | } |
michael@0 | 53 | if (off1 == -1 || off2 == -1) |
michael@0 | 54 | die("Could not determine offset into vtable!"); |
michael@0 | 55 | factor = (off2 - off1) * 4; |
michael@0 | 56 | factorshift = -1; |
michael@0 | 57 | while (factor) { |
michael@0 | 58 | factorshift++; |
michael@0 | 59 | factor >>= 1; |
michael@0 | 60 | } |
michael@0 | 61 | printf("/* Automatically generated by vtable_layout_x86.cpp */\n"); |
michael@0 | 62 | printf("#define GCC_VTABLE_START\t0x%x\n", off1 * 4); |
michael@0 | 63 | printf("#define GCC_VTABLE_FACTOR\t0x%x\n", (off2 - off1) * 4); |
michael@0 | 64 | printf("#define GCC_VTABLE_SHIFT\t0x%x\n", factorshift); |
michael@0 | 65 | exit(0); |
michael@0 | 66 | } |