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