免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3680 | 回复: 3
打印 上一主题 下一主题

求助,关于一个ld的问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-01-18 11:32 |只看该作者 |倒序浏览
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就出现这种情况呢?
谢谢

论坛徽章:
0
2 [报告]
发表于 2008-01-19 00:06 |只看该作者
-fpic
如果支持这种目标机,编译器就生成位置无关目标码.适用于共享库(shared library).
-fPIC
如果支持这种目标机,编译器就输出位置无关目标码.适用于动态连接(dynamic linking),即使分支需要大范围转移

这是GCC手册上关于这两个参数的说明,具体我也不太清楚

论坛徽章:
0
3 [报告]
发表于 2008-01-21 19:35 |只看该作者
PIC代码的生成,貌似是gcc的问题而不是ld的问题

gcc manul上面有说
-fpic        If the GOT size for the linked executable exceeds a machine-specific maximum size, you get an error message from the linker indicating that -fpic does not work; in that case, recompile with -fPIC instead. (These maximums are 8k on the SPARC and 32k on the m68k and RS/6000. The 386 has no such limit.)

-fPIC       If supported for the target machine, emit position-independent code, suitable for dynamic linking and avoiding any limit on the size of the global offset table. This option makes a difference on the m68k, PowerPC and SPARC. Position-independent code requires special support, and therefore works only on certain machines.

关键在于GOT全局偏移量表里面的跳转项大小。
intel处理器应该是统一4字节,没有问题。
powerpc上由于汇编码或者机器码的特殊要求,所以跳转项分为短、长两种。

-fpic为了节约内存,在GOT里面预留了“短”长度。
而-fPIC则采用了更大的跳转项。

具体什么情况我也不了解了。
不知道楼主懂不懂powerpc的汇编,讲解一下powerpc的跳转指令??

论坛徽章:
0
4 [报告]
发表于 2008-01-24 20:58 |只看该作者
楼上正解,楼主的跳转已经超过了32M
加个-mlongcall 编译一下应该可以
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP