Hi, I have a very strange bug. I don't know exactly where the problem can be, if in the fpc compiler, in the sdl libraries, in the pascal sdl headers or what, but I write here first so maybe someone can give me a clue.
I have a program using SDL2 in freepascal, using Tim Blume's SDL2 pascal headers:
https://github.com/ev1313/Pascal-SDL-2-Headers .
What happens is that when I call SDL_FillRect() I get a SIGSEGV.
After debugging and investigating a lot, it seems that SDL_FillRect is using a SSE version. The code in question that causes the problem is this (inside SDL_FillRect, in C):
static void
SDL_FillRect4SSE(Uint8 *pixels, int pitch, Uint32 color, int w, int h)
{
int i, n;
Uint8 *p = NULL;
__m128 c128;
DECLARE_ALIGNED(Uint32, cccc[4], 16);
cccc[0] = color;
cccc[1] = color;
cccc[2] = color;
cccc[3] = color;
c128 = *(__m128 *)cccc;
The segmentation fault happens in the last line because, I suppose, c128 is actually not aligned in 16 bytes and neither cccc: (its address when I debugged was 0xbffff69c) while the address of cccc was (0xbffff60c).
DECLARE_ALIGNED actually is
#define DECLARE_ALIGNED(t,v,a) t __attribute__((aligned(a))) v
that is that gcc is supposed to be aligning that c128 variable by default and cccc because we is is specified. But when I execute the program it is not.
Note that this always works if I make the program in C (at least in the several tests I have done, the variables are aligned, for example addresses end in in 0x...90).
In the case of my free pascal program, this error sometimes happens and sometimes not, depending on the amount of code I write, or maybe is depending on when the shared library is loaded in memory, so I am a bit lost.
The question is: when the SDL dynamic library is loaded, because my pascal program needs it, who is deciding where the library is aligned? Because I suppose that the above DECLARE_ALIGNED directive aligns the stack variables int the function but 'relative' to the function start when it is compiled, but the whole library or the whole function should loaded in memory in some address and maybe someone decides where the whole code is loaded and aligned. Who is deciding that? Is this a problem of fpc in combination of a shared library that requires specific aligment, the SDL2 pascal headers are lacking something, or can it be a problem of the SDL libraries?
I know that the problem is strange, but I am sure the Free Pascal compiler writers have a lot of knowledge about these things, so maybe they have a clue.
Thank you very much.