- 论坛徽章:
- 0
|
powerpc平台Linux (none) 2.6.18.8
#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>
int main(void)
{
void *handle;
void (*errfcn)(const char *fmt, ...);
const char *errmsg;
FILE *pf;
handle = dlopen("./liberr.so", RTLD_NOW);
if(handle == NULL) {
fprintf(stderr, "Failed to load liberr.so: %s\n", dlerror());
exit(EXIT_FAILURE);
}
dlerror();
errfcn = dlsym(handle, "err_ret");
if((errmsg = dlerror()) != NULL) {
fprintf(stderr, "Didn't find err_ret(): %s\n", errmsg);
exit(EXIT_FAILURE);
}
if((pf = fopen("foobar", "r")) == NULL)
errfcn("couldn't open foobar");
dlclose(handle);
return EXIT_SUCCESS;
}
|
ppc_85xx-gcc hello3.c -g -o hello3.exe -ldl
#include <stdio.h>
void err_ret(void* str)
{printf("%s\n", str);}
|
ppc_85xx-gcc -shared liberr.c -o liberr.so
直接运行hello3.exe ,能正常运行
如果:
/lib/ld-2.3.5.so ./hello3.exe
则:
# /lib/ld-2.3.5.so ./hello3.exe
Failed to load liberr.so: ./liberr.so: R_PPC_REL24 relocation at 0x07fdf6e0 for symbol `puts' out of range
如果我这样生成库:
ppc_85xx-gcc -shared -fPIC liberr.c -o liberr.so
就都没有问题了
请问这两种方式有什么区别吗,为何加了-fPIC就出现这种情况呢?
谢谢 |
|