【问题标题】:linux asm problem: calling extern functionlinux asm问题:调用extern函数
【发布时间】:2011-01-20 10:20:53
【问题描述】:

在 Linux 上

file1.s:

.text
.globl MyFunc
Func:
        ....
 call my_jump
 ret  

文件2.h:

extern "C" FUNC_NO_RETURN  void  my_jump();

file3.cpp:

extern "C" __attribute__((noinline)) void my_jump()
{
     return;
}

链接调用“MyFunc”的模块时,我收到以下错误:(之前在 asm 代码中添加对 my_jump 的调用之前,一切正常)

"relocation R_X86_64_PC32 against 'longjmp_hack' can not be used when making a shared object; recompile with -fPIC"

有什么想法吗?

【问题讨论】:

  • 需要更多信息: - 您使用 GCC 还是直接使用 LD 来链接对象?另外,您使用哪些命令行参数? - 为什么是“attribute((noinline))”语句?
  • 尝试-fPIC重新编译了吗???
  • 我正在使用 g++ 进行链接。我发现我需要在 file2.h 中添加 attribute ((visibility ("hidden"))) 我的函数声明。但是,我不知道为什么需要它,因为我在阅读此标志时假设将不同的函数声明聚合到共享对象链接中的同一对象,但我只定义了一次此函数!

标签: linux assembly


【解决方案1】:

从 file2.h 中删除 FUNC_NO_RETURN

例如file2.h:

extern "C" void my_jump();

file4.c:

#include "file2.h"  
extern "C" void MyFunc();  
main(){  
   MyFunc();  
}

并修正 file1.s 中的错字:

.text  
.globl MyFunc  
MyFunc:  
  call my_jump  
  ret  

这一切对我来说都编译得很好......

g++ file1.s file3.cpp file4.c -o a.out

编译器版本;

$ g++ --version
g++ (GCC) 4.6.2 20111027(红帽 4.6.2-1)

Linux版本:3.1.5-6.fc16.x86_64

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-11-24
    • 1970-01-01
    • 2013-02-08
    • 2022-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多