Dinesh Gowda
Dinesh Gowda

Reputation: 1144

Same function name in different so files and both so files linked to a binary

I am working on binutils addr2line and building by own library called address2lineutil.so and my machine already has binutils 2.22 installed installed in /usr/lib.

when I do ldd on my library address2lineutil.so

~/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)

I requires libbfd-2.29.so and is linked properly to my install directory.

I have test program to test my library 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;
}

Now the Problem comes here : The headers Debug.h and DebugSymbolsUnix.h requires libbfd as well so they are linked to my /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)

When I run my program I am getting a segmentation fault because my function call is going to address2lineutil.so from there it is redirecting to bfd-2.22-system.so but it should actually go to libbfd-2.29.so.

How do I make the function call to be called in libbfd-2.29.so.

Here is the backtrace from core dump and 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

Upvotes: 1

Views: 312

Answers (1)

Employed Russian
Employed Russian

Reputation: 213456

How do I make the function call to be called in libbfd-2.29.so.

You don't. UNIX shared libraries don't work the same way as Windows DLLs do, and in particular, your libaddress2lineutil.so doesn't know that it's supposed to call into libbfd-2.29 and not into libbfd-2.22-system. It will call into whatever library first provides the symbol it is trying to call.

You must arrange for the application to link against a single copy of libbfd.

Probably the easiest way to achieve that is not to link the test app against libbfd-2.22-system.so, and link it against libbfd-2.29.so instead.

Upvotes: 1

Related Questions