In my C project, I have a shared library compiled with -fPIC
that contains several static data structures declared at global scope, including, let's say,
static struct mydata;
This library calls a function
void myfunc() {
char foo[2048] = {0};
...
}
which is defined in a different shared library.
When I compile my main application, I link in the shared library that defines myfunc()
. At runtime, the main application loads the other shared library containing the static data structures using dlopen()
.
Calling myfunc()
from the shared library in which mydata
is declared results in mydata
being at least partially overwritten.
After snooping with gdb
it is clear that the local array foo
is placed in memory in such a way that it overlaps with the address of mydata
, so when the array is initialized to 0, so too is mydata
.
How could this be? Am I doing something that is unhandled by the compiler?
Note that I am running Red Hat on 64-bit architecture using gcc 4.6.