1

i am cross compiling a program for a embedded system. The program uses a shared library, which i openend like this.

#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>      //needed for dynamic linking

void  *FunctionLib;     //Handle to shared lib file
int   (*Function)();    //Pointer to loaded routine
const char *dlError;    //Pointer to error string

int main( argc, argv )
{
    int   rc;               //return codes
    printf("start...\n");

    //Open Dynamic Loadable Libary with absolute path
    FunctionLib = dlopen("/lalabu/sharedLib.so",RTLD_LAZY | RTLD_GLOBAL);
    dlError = dlerror();
    printf("Open sharedLib.so returns: %s \n", dlError);
    if( dlError ) exit(1);

    //Find function
    Function = dlsym( FunctionLib, "getSomething");
    dlError = dlerror();
    printf("Find symbol getSomething returns: %s \n", dlError);
    if( dlError ) exit(1);
...

I am compiling the code with the following command

mips-linux-uclibc-gcc -Wall -ldl ./dynamic_linking.c -o /dynamic_linking

which works without any warnings and stuff.
If i now try to execute this code on my device, i get the following error:

# ./dynamic_linking
start...
Open sharedLib.so returns: (null)
Find symbol getSomething returns: Unable to resolve symbol
./dynamic_linking: can't resolv '_ZNSt8ios_base4InitD1Ev'

If i take a look with IDA which functions sharedLib.so wants to import, i see the Function (or symbol?) '_ZNSt8ios_base4InitD1Ev' and where it should be (libc.so.0).
If i take a look at libc.so.0, again with IDA, i don't see a function called like this. Also nothing like ios_base, ios or base.
I tried already various flag combinations with dlopen() which resulted always in the above error, except when i use RTLD_NOW instead of RTLD_LAZY i get

Segmentation fault (core dumped)

instead of

./dynamic_linking: can't resolv '_ZNSt8ios_base4InitD1Ev'

Furthermore i tried a few gcc linking-options like rdynamic, always with the same result.
Moreover i tried to use a shared library which doesn't use _ZNSt8ios_base4InitD1Ev, the 'libc.so.0' to check if my c-code is working. I only changed the name and the absolute path and deleted the find function part. It worked without errors.
As you may recognize from my post, i just started cross compiling and using dynamic libraries, so maybe my fault is somewhere else. Besides, I am not sure if i even understand where the problem is, so any hints are welcome. If you need further information, i'd be happy to give them to you.
Greetings, Pingu

Pingu
  • 31
  • 1
  • 7

1 Answers1

3

That _ZNSt8ios-base4InitD1Ev is not a C symbol but a C++ one.

$ echo _ZNSt8ios-base4InitD1Ev | c++filt

Gives no clue, but if you replace the - with a _ (a typo, maybe?):

$ echo _ZNSt8ios_base4InitD1Ev | c++filt
std::ios_base::Init::~Init()

So that is the destructor of an internal class of the C++ STD library. So you should check the libstdc++.so library instead libc.so.

My advice is to just compile your program using G++, so the C++ library is properly initialized. It is not intended to be loaded dynamically, and that's why the segmentation fault.

rodrigo
  • 94,151
  • 12
  • 143
  • 190
  • Thanks for the answer, i compiled the program with mips-linux-uclibc-cpp -Wall ./dynamic_linking.c -o ./dynamic_linking and it compiled whihtout any error. I can check tomorrow if it works on the device, i will give feedback. The - instead of the _ was indeed a typo, sorry. – Pingu Dec 17 '12 at 22:50
  • @Pingu: But beware! `cpp` is not the name of the GCC C++ compiler (that would be `g++` or `c++`. It is the name of the C precompiler. It would be funny if `dynamic_linking` program ends up being the output of the precompiler! – rodrigo Dec 17 '12 at 22:59
  • I dont have a gcc c++ compiler for my device, but reading this post, http://stackoverflow.com/questions/6626363/why-use-g-instead-of-gcc-to-compile-cc-files i think its possible to compile the code with gcc using the c++ std libraries. i'll try to figure it out. – Pingu Dec 18 '12 at 09:53
  • finally got it working with gcc and a whole lot of flags using the libstdc++. thanks rodrigo – Pingu Dec 19 '12 at 12:00