making aliases into the middle of a structure

Zack Weinberg zack@rabi.columbia.edu
Thu Apr 22 11:13:00 GMT 1999


Consider this bit of code.

struct s
{
  int a, b;
};

struct s x = { 0, 0 };

extern __typeof(x.b) y __attribute__ ((alias("x.b")));

The goal is to have x.b and y refer to the same memory location.  I am
aware that this is somewhat perverse, but there are legitimate reasons
(binary compatibility) for it.

If gcc were to translate this into

.data
.globl x
        .align 4
        .type    x,@object
        .size    x,8
x:
        .zero   8
.globl y
        .set    y, x+4

then the assembler would generate a correct object module, and
references to `x.b' and `y' would indeed operate on the same memory
location.

Unfortunately this is not what gcc produces; instead I get

.data
.globl x
        .align 4
        .type    x,@object
        .size    x,8
x:
        .zero   8
.globl y
        .set    y,x.b


The assembler generates an undefined reference to the symbol `x.b' and
forgets about `y'.

I have experimented with workarounds involving offsetof();
unfortunately, the best this can do is

	.set y,x + ((size_t) &((s *)0)->b)

which is not an expression that the assembler can evaluate.  I do not
want to write a raw assembly module, because that will break if the
padding inside the structure, or the sizes of the component types,
change.

Any suggestions?

zw


More information about the Gcc mailing list