不同的so文件中的函数名称相同,因此文件链接到二进制文件

时间:2017-09-07 06:22:05

标签: c++ segmentation-fault shared-libraries

我正在使用binutils addr2line并使用名为address2lineutil.so的自有库构建,我的机器已安装了/ usr / lib中安装的binutils 2.22。

当我在我的库地址2lineutil.so

上做ldd时
~/dev/binutilsinstaller_TEST_6917/lib$ ldd libaddress2lineutil.so
        linux-gate.so.1 =>  (0xb774e000)
        libbfd-2.29.so => /home/dinesh/dev/binutilsinstaller_TEST_6917/lib/libbfd-2.29.so (0xb7622000)
        libdl.so.2 => /lib/i386-linux-gnu/i686/cmov/libdl.so.2 (0xb75f3000)
        libgcc_s.so.1 => /lib/i386-linux-gnu/libgcc_s.so.1 (0xb75d5000)
        libc.so.6 => /lib/i386-linux-gnu/i686/cmov/libc.so.6 (0xb7471000)
        /lib/ld-linux.so.2 (0xb774f000)

我需要libbfd-2.29.so并且正确链接到我的安装目录。

我有测试程序来测试我的库TestBacktraceLib.cpp:

#include "address2lineutil.h"
#include <iostream>
#include <string>
#include <stdlib.h>
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "AppContext.h"
#include "DebugSymbolsUnix.h"
#include "Debug.h"
#include "Util.h"

using namespace std;

void fun4(){
char filename[256];
int line;
GetBackTrace(filename,&line, 0);
cout << "File : " << filename << endl;
cout << "Line :: " << line << endl;
}

void fun3(){
fun4();
}

void fun2(){
fun3();
}

void fun1(){
fun2();
}

int main(){
fun1();
return 0;
}

现在问题来了: Debug.h和DebugSymbolsUnix.h标题也需要libbfd,所以它们链接到我的/ usr / lib。

ldd on my TestProgram:

$ ldd TestBacktraceLib
        libbfd-2.22-system.so => /usr/lib/libbfd-2.22-system.so (0xb48d6000)
        libaddress2lineutil.so.0 => /home/dinesh/dev/binutilsinstaller_TEST_298/lib/libaddress2lineutil.so.0 (0xb77b8000)
        libicuuc.so.48 => /usr/lib/i386-linux-gnu/libicuuc.so.48 (0xb476c000)
        libicudata.so.48 => /usr/lib/i386-linux-gnu/libicudata.so.48 (0xb35fb000)
        /lib/ld-linux.so.2 (0xb77c5000)
        libicui18n.so.48 => /usr/lib/i386-linux-gnu/libicui18n.so.48 (0xb3423000)
        liblzma.so.5 => /lib/i386-linux-gnu/liblzma.so.5 (0xb33fc000)
        libboost_thread.so.1.49.0 => /usr/lib/libboost_thread.so.1.49.0 (0xb33e3000)
        libgcrypt.so.11 => /lib/i386-linux-gnu/libgcrypt.so.11 (0xb335d000)
        libresolv.so.2 => /lib/i386-linux-gnu/i686/cmov/libresolv.so.2 (0xb3349000)
        libsasl2.so.2 => /usr/lib/i386-linux-gnu/libsasl2.so.2 (0xb332d000)
        libgnutls.so.26 => /usr/lib/i386-linux-gnu/libgnutls.so.26 (0xb3264000)
        libkrb5.so.3 => /usr/lib/i386-linux-gnu/libkrb5.so.3 (0xb3192000)
        libk5crypto.so.3 => /usr/lib/i386-linux-gnu/libk5crypto.so.3 (0xb3167000)
        libcom_err.so.2 => /lib/i386-linux-gnu/libcom_err.so.2 (0xb3162000)
        libkrb5support.so.0 => /usr/lib/i386-linux-gnu/libkrb5support.so.0 (0xb3159000)
        libkeyutils.so.1 => /lib/i386-linux-gnu/libkeyutils.so.1 (0xb3154000)
        libgpg-error.so.0 => /lib/i386-linux-gnu/libgpg-error.so.0 (0xb3150000)
        libtasn1.so.3 => /usr/lib/i386-linux-gnu/libtasn1.so.3 (0xb313d000)
        libp11-kit.so.0 => /usr/lib/i386-linux-gnu/libp11-kit.so.0 (0xb312b000)

当我运行我的程序时,我遇到了一个分段错误,因为我的函数调用将从那里转发到address2lineutil.so,它将重定向到bfd-2.22-system.so,但它实际上应该转到libbfd-2.29.so。 / p>

如何在libbfd-2.29.so中调用函数调用。

这是来自核心转储和gdb的回溯:

(gdb) bt
#0  0xb79c8850 in ?? ()
#1  0xb72ea5e8 in bfd_map_over_sections () from /usr/lib/libbfd-2.22-system.so
#2  0xb7a0413e in translate_addresses (abfd=0x804b670, section=0x0) at address2lineutil.cpp:205
#3  0xb7a044ea in process_file (file_name=0xbfffebb8 "/home/dinesh/a.out", section_name=0x0, target=0x0) at address2lineutil.cpp:345
#4  0xb7a045eb in address2line (addressofprocess=0xbfffefb8, binaryname=0xbfffebb8 "/home/dinesh/a.out") at address2lineutil.cpp:393
#5  0xb7a04c31 in GetBackTrace (filepath=0xbffff00c "00", functionline=0xbffff10c, skip=0) at address2lineutil.cpp:477
#6  0x08048b53 in fun4 () at /home/dinesh/TestBacktraceLib.cpp:19
#7  0x08048bda in fun3 () at /home/dinesh/TestBacktraceLib.cpp:25
#8  0x08048be7 in fun2 () at /home/dinesh/TestBacktraceLib.cpp:29
#9  0x08048bf4 in fun1 () at /home/dinesh/TestBacktraceLib.cpp:33
#10 0x08048c01 in main (argc=1, argv=0xbffff204) at /home/dinesh/TestBacktraceLib.cpp:45

1 个答案:

答案 0 :(得分:1)

  

如何在libbfd-2.29.so中调用函数调用。

你没有。 UNIX共享库的工作方式与Windows DLL的工作方式不同,特别是,libaddress2lineutil.so 不知道它应该调用libbfd-2.29而不是{ libbfd-2.22-system。它将调用任何库,首先提供它试图调用的符号。

必须安排应用程序链接到libbfd的单个副本。

实现这一目标的最简单方法可能不是将测试应用与libbfd-2.22-system.so相关联,而是将其与libbfd-2.29.so相关联。