2

I know this is very hard to do, and that I should avoid that, but I have my reasons for this. I want to modify the order of some field declarations in compilation time, for example :

class A {
  char c;
  int i;
}

must turn to :

class A {
      int i;
      char c;
}

if I chose to swap the order of i and c, I want to know how to change the location of a field declaration having its tree

Anyone know how can I do this ?? thanks !

I use the g++ 4.9.2 version of plugins

artless noise
  • 21,212
  • 6
  • 68
  • 105
Othman Benchekroun
  • 1,998
  • 2
  • 17
  • 36
  • Can you encapsulate the variables in a class? You get a guaranteed initialisation order with classes. – Andy Brown Apr 07 '15 at 10:12
  • I want to give my own order to the variables, maybe I wasn't so clear, I want to write a gcc plugin : https://gcc.gnu.org/wiki/plugins if you want to know more about gcc plugins. The plugin would allow me to change the order of declaration of my variables. – Othman Benchekroun Apr 07 '15 at 11:42

2 Answers2

3

If I was going to try this, I would try two different approaches.

  1. Hook in to the PLUGIN_FINISH_TYPE event and rewrite the type there. To rewrite it, reorder the fields and force a relayout of the type. You'll have to read a bit of GCC source to understand how to invalidate the layout and force a new one.

  2. If that didn't work, add a new pass that is run just after gimplification, and try to rewrite the types there. I suspect this is not likely to work, though.

Tom Tromey
  • 21,507
  • 2
  • 45
  • 63
0
  1. Hook in to the PLUGIN_FINISH_TYPE event and rewrite the type there. To rewrite it, reorder the fields and force a relayout of the type. You'll have to read a bit of GCC source to understand how to invalidate the layout and force a new one.

This is implemented in randomize_layout_plugin.c in linux kernel.

This solution works but it breaks down dwarf debug information. Actually, in debug information, the order of members stay the same one as initially defined in the source code, but the structure is well shuffled in the binary.

creyD
  • 1,972
  • 3
  • 26
  • 55
roskad
  • 1