Back to index

lightning-sunbird  0.9+nobinonly
vtable_layout_x86.cpp
Go to the documentation of this file.
00001 /* this code contributed by Bert Driehuis <bert_driehuis@nl.compuware.com> */
00002 
00003 #include <stdio.h>
00004 
00005 // Try to determine the vtable layout generated by G++
00006 // Produces the offset at which the first vtable entry can be
00007 // found, and the factor to apply for subsequent entries on stdout.
00008 // Example output:
00009 //    #define GCC_VTABLE_START        0xc
00010 //    #define GCC_VTABLE_FACTOR       0x8
00011 
00012 class test {
00013 public:
00014       virtual int     t1(void);
00015       virtual int     t2(void);
00016       int             x;
00017 };
00018 
00019 test::test() { this->x = 0x12121212; };
00020 
00021 int test::t1(void) { return 1; }
00022 int test::t2(void) { return 2; }
00023 
00024 void die(char *x) {
00025       fprintf(stderr, "%s\n", x);
00026       exit(1);
00027 }
00028 
00029 int 
00030 main()
00031 {
00032       int             i;
00033       test           *t = new test();
00034       int            *tp = (int *) t;
00035       int             off1 = -1;
00036       int             off2 = -1;
00037       int             factor;
00038       int             factorshift;
00039 
00040       if (*tp++ != 0x12121212)
00041               die("Integer element test::x not found!");
00042       tp = (int *) *tp;
00043       for (i = 0; i < 10; i++) {
00044               if (tp[i] == (int) t->t1)
00045                       off1 = i;
00046               if (tp[i] == (int) t->t2)
00047                       off2 = i;
00048       }
00049       if (off1 == -1 || off2 == -1)
00050               die("Could not determine offset into vtable!");
00051       factor = (off2 - off1) * 4;
00052       factorshift = -1;
00053       while (factor) {
00054               factorshift++;
00055               factor >>= 1;
00056       }
00057       printf("/* Automatically generated by vtable_layout_x86.cpp */\n");
00058       printf("#define GCC_VTABLE_START\t0x%x\n", off1 * 4);
00059       printf("#define GCC_VTABLE_FACTOR\t0x%x\n", (off2 - off1) * 4);
00060       printf("#define GCC_VTABLE_SHIFT\t0x%x\n", factorshift);
00061       exit(0);
00062 }