[PAHOLE]: Check structure definitions in multi-object files

An example to illustrate the kind of checks done:

[acme@newtoy multi-cu]$ cat a.c

struct foo {
	int	a;
	char	b;
};

void a_foo_print(struct foo *f)
{
	printf("f.a=%d\n", f->a);
}

[acme@newtoy multi-cu]$ cat main.c
struct foo {
	int	a;
	char	b;
	char	c;
};

extern void a_foo_print(struct foo *f);

int main(void)
{
	struct foo f = { .a = 10, };
	a_foo_print(&f);
	return 0;
}

[acme@newtoy multi-cu]$ cc -g -c a.c -o a.o
[acme@newtoy multi-cu]$ cc -g -c main.c -o main.o
[acme@newtoy multi-cu]$ cc a.o main.o -o m
[acme@newtoy multi-cu]$ pahole m
class: foo
first: a.c
current: main.c
nr_members: 2 != 3
padding: 3 != 2

[acme@newtoy multi-cu]$

Gotcha? In the above case this inconsistency wouldn't cause problems, as the
'c' member doesn't makes the struct bigger, it uses the padding, but what if we
inverted the members 'a' and 'b'?

Upcoming csets will check if the type and order of the members are the same,
should help in some complex projects where people insist on using #ifdefs in
struct definitions.

Signed-off-by: Arnaldo Carvalho de Melo <acme@mandriva.com>
This commit is contained in:
Arnaldo Carvalho de Melo 2006-12-18 11:05:07 -02:00
parent afb41b16f9
commit e87d958939
1 changed files with 16 additions and 0 deletions

View File

@ -162,6 +162,22 @@ static void class__chkdupdef(const struct class *self, struct class *dup)
class__dupmsg(self, dup, &hdr, "size: %llu != %llu\n",
self->size, dup->size);
if (self->nr_members != dup->nr_members)
class__dupmsg(self, dup, &hdr, "nr_members: %u != %u\n",
self->nr_members, dup->nr_members);
if (self->nr_holes != dup->nr_holes)
class__dupmsg(self, dup, &hdr, "nr_holes: %u != %u\n",
self->nr_holes, dup->nr_holes);
if (self->nr_bit_holes != dup->nr_bit_holes)
class__dupmsg(self, dup, &hdr, "nr_bit_holes: %u != %u\n",
self->nr_bit_holes, dup->nr_bit_holes);
if (self->padding != dup->padding)
class__dupmsg(self, dup, &hdr, "padding: %u != %u\n",
self->padding, dup->padding);
/* XXX put more checks here: member types, member ordering, etc */
if (hdr)