Symbols in dylib, Works with gcc-4.0 not in gcc-4.2 (OSX-default)
i'v got an dynamic-Lib which i build with
OBJECTS=keys.o etc2.o foo.o
$(CC) -DSYS_MACOSX -g -fPIC -flat_namespace -L. -lpthread -Wl,-flat_namespace -dynamiclib -shared -o libmylib.dylib $(OBJECTS)
My test progam links with this library
$(CC) -DSYS_MACOSX -g -fPIC testmain.c -o testmain -I. -flat_namespace -L. -lpthread -lmylib
When CC=gcc-4.2 I get following error in gdb, when I try to access const symbols in libmylib.dylib:
Program received signal EXC_BAD_ACCESS, Could not access memory.
Reason: KERN_INVALID_ADDRESS at address: 0x0000000000112dd0
0x0000000100004ec7 in extractKeyFromList (keyList=0x112dd0, key=0x1002001e0 "Adresse1.aName1") at keys.c:148
The programm works, with:
- gcc-4.0 on SnowLeopard
- gcc-4.3.2 on Debian i386
- gcc-4.1.2 for arm-angstrom-linux-gnueabi
Update: Here is some debug-output main=main loadKeyList=function in Lib
GCC 4.0:
main: sizeof KeyList = 149480
loadKeyList: sizeof KeyList = 149480
loadKeyList: list at 0xfe88c
loadKeyList: 开发者_开发知识库 sizeof list = 149480
loadKeyList: list->count 3086
main: sizeof handle->keyList = 149480
main: handle->keyList at 0xfe88c
main: handle->keyList->count 3086
GCC4.2
(gdb) run
Starting program: keyextractor -k Adresse1.aName1
Reading symbols for shared libraries ++. done
main: sizeof KeyList = 166088
loadKeyList: sizeof KeyList = 166088
loadKeyList: list at 0x112dd0
loadKeyList: sizeof list = 166088
loadKeyList: list->count 3086
main: sizeof handle->keyList = 166088
main: handle->keyList at 0x112dd0
Program received signal EXC_BAD_ACCESS, Could not access memory.
Reason: KERN_INVALID_ADDRESS at address: 0x0000000000112dd0
0x0000000100001940 in main (argc=3, argv=0x7fff5fbfefb8) at keyextractor.c:110
110 printf("main: handle->keyList->count %i\n", handle->keyList->count);
(gdb)
The Struct looks like this:
typedef struct _KeyList {
int count;
Key keys[4152];
} KeyList;
Update 2: This works in gcc-4.2 from main
printf("KEYMAP.keyList[5] at 0x%x count = %i\n", &KEYMAP.keyList[5], KEYMAP.keyList[5].count);
Output: KEYMAP.keyList[5] at 0x112dd0 count = 3086
That's the same adress like pointer handle->keyList!
KEYMAP looks like:
typedef struct _KeyMapList {
int count;
KeyList keyList[];
} KeyMapList;
const KeyMapList KEYMAP = {
.count = 6,
.keyList = {
{ ... }, { .... },
{ ... }, { .... },
{ ... }, { .count=3086, keys.... }
}
};
So whats the problem with gcc-4.2?
Thanks
ok .. it seems not to be a problem with gcc.
It's a problem with the x86_64-Architecture. Building with "-arch i386" solves the problem with gcc4.2.
I still don't know, why it doesn't work with x86_64. Both binary and dylib are x86_64:
libmylib.dylib: Mach-O 64-bit dynamically linked shared library x86_64
keyextractor: Mach-O 64-bit executable x86_64
My program failed on Linux-x86_64, too
But the real answer is: you should listen to gcc warnings and compile with -Wall
keyextractor.c:114: warning: cast to pointer from integer of different size
This warning was in the line, where I called a function from the Lib, without #include-ing the respective Header-File. Without function-declartion it worked:
- i386-architecture as shared lib and static
- x86_64-architecture only static
So, after including the right header-file, everything worked fine.
精彩评论