From mboxrd@z Thu Jan 1 00:00:00 1970 From: Zack Weinberg To: egcs@egcs.cygnus.com Subject: making aliases into the middle of a structure Date: Fri, 30 Apr 1999 23:15:00 -0000 Message-ID: <199904221813.OAA07239@blastula.phys.columbia.edu> X-SW-Source: 1999-04n/msg00769.html Message-ID: <19990430231500.MglaktnoAISrnYZxp_L6eXrHB6wrMptpNOOz2bu1PsM@z> 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