https://gcc.gnu.org/bugzilla/show_bug.cgi?id=123033
Bug ID: 123033
Summary: gcc generates misaligned SSE
Product: gcc
Version: 13.2.1
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c
Assignee: unassigned at gcc dot gnu.org
Reporter: wuz73 at hotmail dot com
Target Milestone: ---
I have some code that is necessary to use x86 assembly. Following is a
simplified version.
#include <stdio.h>
#include <stdarg.h>
static void* flag = NULL;
void print0(const char* fmt, ...)
{
va_list args;
va_start(args, fmt);
vprintf(fmt, args);
va_end(args);
}
void print(const char* fmt, ...)
{
asm volatile (
"mov %[flag_var], %%r10\n"
"test %%r10, %%r10\n"
"jne 1f\n"
"call print0\n"
"1:\n"
:
: [flag_var] "m" (flag)
: "r10", "memory"
);
}
int main()
{
double a=1,b=2;
print("%g %g\n", a,b);
}
When compiled with gcc, the generated code segfault due to misaligned SSE
instructions:
...
40115d: 74 20 je 40117f <print0+0x59>
40115f: 0f 29 45 80 movaps %xmm0,-0x80(%rbp)
401163: 0f 29 4d 90 movaps %xmm1,-0x70(%rbp)
...
Adding __attribute__((force_align_arg_pointer)) to print0 worked around the
issue but still segfault with "gcc -O2". In fact, with -O2, the two floating
point variables (a,b) are not even loaded into xmm0,xmm1.