pahole: Add --inner_anon option

Allow making the inner struct/enum/union anonymous. This permits using
the header to inspect pointer values using -E, without having to care
about avoiding duplicate type definitions such as:

    struct foo { ... };
    struct bar {
        struct foo {
	     ....
	} a;
    };

With --inner_anon, the conflict between the two definitions of struct
foo is gone:

    struct foo { ... };
    struct bar {
        struct {
	     ....
	} a;
    };

Committer testing:

  $ cat inner_anon.c

  struct foo {
  	int  a;
  	char b;
  };

  struct bar {
          struct foo c;
  	int	   d;
  } bla;
  $ gcc -g -c inner_anon.c   -o inner_anon.o

No expansion:

  $ pahole inner_anon.o
  struct foo {
  	int                        a;                    /*     0     4 */
  	char                       b;                    /*     4     1 */

  	/* size: 8, cachelines: 1, members: 2 */
  	/* padding: 3 */
  	/* last cacheline: 8 bytes */
  };
  struct bar {
  	struct foo                 c;                    /*     0     8 */

  	/* XXX last struct has 3 bytes of padding */

  	int                        d;                    /*     8     4 */

  	/* size: 12, cachelines: 1, members: 2 */
  	/* paddings: 1, sum paddings: 3 */
  	/* last cacheline: 12 bytes */
  };

Expanding types:

  $ pahole -E inner_anon.o
  struct foo {
  	int                        a;                                                    /*     0     4 */
  	char                       b;                                                    /*     4     1 */

  	/* size: 8, cachelines: 1, members: 2 */
  	/* padding: 3 */
  	/* last cacheline: 8 bytes */
  };
  struct bar {
  	struct foo {
  		int                a;                                                    /*     0     4 */
  		char               b;                                                    /*     4     1 */
  	}c; /*     0     8 */

  	/* XXX last struct has 3 bytes of padding */

  	int                        d;                                                    /*     8     4 */

  	/* size: 12, cachelines: 1, members: 2 */
  	/* paddings: 1, sum paddings: 3 */
  	/* last cacheline: 12 bytes */
  };

Anonymising the inner struct:

  $ pahole -E --inner_anon inner_anon.o
  struct foo {
  	int                        a;                                                    /*     0     4 */
  	char                       b;                                                    /*     4     1 */

  	/* size: 8, cachelines: 1, members: 2 */
  	/* padding: 3 */
  	/* last cacheline: 8 bytes */
  };
  struct bar {
  	struct /* foo */ {
  		int                a;                                                    /*     0     4 */
  		char               b;                                                    /*     4     1 */
  	}c; /*     0     8 */

  	/* XXX last struct has 3 bytes of padding */

  	int                        d;                                                    /*     8     4 */

  	/* size: 12, cachelines: 1, members: 2 */
  	/* paddings: 1, sum paddings: 3 */
  	/* last cacheline: 12 bytes */
  };

Signed-off-by: Douglas Raillard <douglas.raillard@arm.com>
Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
[ Added man page entry for --inner_anon, refreshed the patch to cope with the btf_tag series ]
Link: https://lore.kernel.org/all/20211019100724.325570-3-douglas.raillard@arm.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2 files changed