Linux: dlopen fails to find symbols


 
Thread Tools Search this Thread
Top Forums Programming Linux: dlopen fails to find symbols
# 1  
Old 05-17-2010
Linux: dlopen fails to find symbols

I've attached a tar.gz containing three tests to demonstrate the problem I'm having.

Within the tar are two shared libraries, two test applications, and a Makefile that builds the three tests.

The shared libaries are libshlib1.so and libshlib2.so. Both export a function, libFunc, which takes a char * argument, buf, and returns it after sprintf'ing an id and the integer result of an external function, mainFunc. The code for both is identical except for the id to differentiate. The function, mainFunc, is located in the main binary that links (or dlopens) the shared library.

The two test applications are test1.c and test2.c. The first, test1.c, simply contains the function mainFunc and calls the library function libFunc. It's really only there to test that the shared library linked normally will find the reference upon load time. This code is compiled to test1.

The second, test2.c, is where the problem lies. It is compiled twice, once linking shlib1 (test2) and the second linking nothing (test3). The code uses dlopen to open the second shared library, shlib2, and upon successfully loading the library it calls libFunc as retrieved by dlsym.

So...now to the problem. Here is the output of the three executables:

Code:
>test1
SHLIB1:1
>test2
Loading shared library [./libshlib2.so]...OK.
SHLIB2:1
>test3
Loading shared library [./libshlib2.so]...ERROR
  [./libshlib2.so: undefined symbol: mainFunc]

As you can see, test3 fails. However, test2, which is identical to test3 except that it links a different shared libarry than it dlopens, succeeds. Oddly, just linking the other library (shlib1) seems to make the linker decide to "promote" the symbol (mainFunc), making it available to shlib2. However, without that link, test3 fails.

Does anyone know why this would be? I compared the output of nm on both test2 and test3 and, except for the addresses, they are identical. I would understand test3 failing if test2 also failed, because neither actually linked the library they dlopen. But, test2 works just fine. Like I said, it's as if just linking a shared library that uses the symbol mainFunc is enough to change something, but I can't see that change in nm's output as shown below:

Code:
>nm test2 | grep mainFunc
08048604 T mainFunc
>nm test3 | grep mainFunc
08048534 T mainFunc

Any ideas?

Last edited by DreamWarrior; 05-17-2010 at 08:13 PM..
# 2  
Old 05-18-2010
Quote:
Originally Posted by DreamWarrior
The function, mainFunc, is located in the main binary that links (or dlopens) the shared library.
I don't believe shared libraries are intended to be able to link backwards in that manner. If you must have the shared library call a local function, pass it as a function pointer.
# 3  
Old 05-18-2010
Quote:
Originally Posted by Corona688
I don't believe shared libraries are intended to be able to link backwards in that manner. If you must have the shared library call a local function, pass it as a function pointer.
But why, when I link the other library, does it work? That seems odd to me.
# 4  
Old 05-18-2010
Assuming you are using gcc, change your Makefile rule for test3 to include the following linker directive
Code:
-Wl,-export-dynamic

and test3 now works as expected.

Regular executables such as test3 do not export their variables and routines as dynamic symbols by default. You need to use the -export-dynamic directive to specifically export the variables and routines.
# 5  
Old 05-18-2010
In test2.c file the dlopen call should be...
Code:
if ((dl = dlopen(path, RTLD_NOW | RTLD_GLOBAL)) == NULL)

# 6  
Old 05-28-2010
Quote:
Originally Posted by fpmurphy
Assuming you are using gcc, change your Makefile rule for test3 to include the following linker directive
Code:
-Wl,-export-dynamic

and test3 now works as expected.

Regular executables such as test3 do not export their variables and routines as dynamic symbols by default. You need to use the -export-dynamic directive to specifically export the variables and routines.
AWESOME! Thanks! I knew there must have been something about the symbols not getting exported, but I didn't know how to fix it.

Edit: I presume, then, that this is why the version that linked the shared library worked, because the linker knew shared libraries were involved with that executable and, thusly, chose to export the symbols. Is there a way I can tell with nm (or some other tool) whether or not the symbols are exported? As I said, I didn't see a difference in nm output...but, maybe I wasn't looking in the right place or asking for the right thing?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find command works on Linux but fails on Solaris.

Hi, I am looking for a generic find command that works on both Linux and Solaris. I have the below command that works fine on Linux but fails on solaris.find /web/config -type f '(' -name '*.txt' -or -name '*.xml' -name '*.pro' ')' Fails on SunOS mysolaris 5.10 Generic_150400-61 sun4v sparc... (1 Reply)
Discussion started by: mohtashims
1 Replies

2. Shell Programming and Scripting

find command fails mid-way during execution

I wish to search for a particular string say "Yellow_Colors" in all files in all folders in the current directory. Below is the command I use: find ./ -type f | xargs grep -i "Yello_Colors"However, my command does not traverse all files and folders and errors out mid-way with the below error... (7 Replies)
Discussion started by: mohtashims
7 Replies

3. Shell Programming and Scripting

sed works on Linux but fails on Solaris

Hi, On Linux i get the desired ouput: echo "<value>WEB_USER</value>" | sed 's/\(<value>\|<\/value>\)//g'Output: Executing the same command on Solaris: echo "<value>WEB_USER</value>" | sed 's/\(<value>\|<\/value>\)//g'Output: I need to get the desired output on Solaris i.e. WEB_USER and... (4 Replies)
Discussion started by: mohtashims
4 Replies

4. UNIX for Dummies Questions & Answers

Tcp connection to Linux server fails

I am trying to send json messages to a port on a linux server from a remote server running a .net program. I have one implementation running with successful incoming messages to port 1514. I tried to replicate the same thing but just to another port but cannot get it to work as I get the following... (3 Replies)
Discussion started by: unienewbie
3 Replies

5. UNIX for Dummies Questions & Answers

Find command fails when a space is in the directory path variable

I have a script like this running under OS X 10.8. The problem arises when the find command encounters a space in the path name. I need the "dir" variable as I'll be extending the script to more general use. #!/bin/bash CFS=$IFS IFS=$(echo) set dir = "/Users/apta/Library/Mail\... (3 Replies)
Discussion started by: apta
3 Replies

6. UNIX for Advanced & Expert Users

Difference between Unix and Linux for resolving symbols

I came across a difference between Unix and Linux, when it comes to resolving the symbols from the libs. consider the following code segments... $ cat call1.c #include <stdio.h> int a1; extern int a3; void prnt_a3() { printf("\n%d\n",a3); } $ cat test.c #include <stdio.h>... (12 Replies)
Discussion started by: snowline84
12 Replies

7. UNIX for Advanced & Expert Users

Grub installation fails in Linux BMR..

Hi all, This topic is purely on a problem in installing grub in my LINUX BMR process... A major problem too.. I am designing Linux BMR where i do the following.. Backup the " / " as a whole .. with partition details ..etc And using the Knoppix Live CD i first create the partitions... (1 Reply)
Discussion started by: selvarajvs
1 Replies

8. Programming

dlopen Linux vs. OSX

I've got a C++ program and some plugins ( shared objects ) that I have been developing for Linux. I was originally using and OSX machine to develop and test most of the code. I use dlopen to open the shared objects and then call methods from them. It behaves differently on Linux ( don't know the... (3 Replies)
Discussion started by: rfkrakora
3 Replies

9. AIX

Find command fails in crontab

Hi , I imported find command I have on my hp-ux server to clean up the /tmp of my new IBM AIX servers. Though, the commands always fails in the cron but if I past it at the prompt, it works find. I tried with at jobs and regular 'find' . Could anyone tell me what I am doing wrong? Many... (4 Replies)
Discussion started by: cforget2810
4 Replies

10. HP-UX

test program(dlopen) fails on hp but run well on solaris

Hi, I have a c test program which test dlopen call. This program run well on solaris but fails on hp-ux. The program load jvm library successfully on solaris. On hp-ux it displays error I compile this program as $cc -o testjvm testjvm.c What am I missing? I have tried... (2 Replies)
Discussion started by: shriashishpatil
2 Replies
Login or Register to Ask a Question