https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107339
--- Comment #2 from yy <yy172179 at 163 dot com> ---
(In reply to Andrew Pinski from comment #1)
> The code is broken.
>
> uint32_t here is aligned to 32bits and you access it via that type.
>
>
> this is the correct fix:
>
>
> typedef uint32_t uint32_t_ua __attribute__((aligned(1)));
>
> void tttt (misalign_t* t){
>
> printf("%x\n",(*(const uint32_t_ua *)&t->rseq[0]));
> printf("%x\n",(*(const uint32_t_ua *)&t->rseq[1]));
> }
(In reply to Andrew Pinski from comment #1)
> The code is broken.
>
> uint32_t here is aligned to 32bits and you access it via that type.
>
>
> this is the correct fix:
>
>
> typedef uint32_t uint32_t_ua __attribute__((aligned(1)));
>
> void tttt (misalign_t* t){
>
> printf("%x\n",(*(const uint32_t_ua *)&t->rseq[0]));
> printf("%x\n",(*(const uint32_t_ua *)&t->rseq[1]));
> }
Maybe I didn't make the problem clear. To add, I think the non aligned address
"lw" will be converted to "lbu" to access this address indirectly. Change the
above code parameters to the structure, and the situation I think is correct
will occur.
#include<stdio.h>
typedef struct __attribute__((packed)) misalign
{
uint8_t rseq[16];
uint8_t type;
uint8_t cnt;
} misalign_t;
void tttt (misalign_t t){
printf("%x\n",(*(const uint32_t*)&t.rseq[0]));
printf("%x\n",(*(const uint32_t*)&t.rseq[1]));
}
int main()
{
misalign_t abc;
misalign_t *test=&abc;
tttt(abc);
while(1);
}