Forums

Skip to content

Advanced search
  • Quick links
    • Unanswered topics
    • Active topics
    • Search
  • FAQ
  • Login
  • Register
  • Board index Assistance Portage & Programming
  • Search

Duplicate symbol names in shared library

Problems with emerge or ebuilds? Have a basic programming question about C, PHP, Perl, BASH or something else?
Post Reply
Advanced search
5 posts • Page 1 of 1
Author
Message
cerealito
n00b
n00b
User avatar
Posts: 20
Joined: Sat Apr 10, 2004 5:09 pm
Location: Toulouse, Fr
Contact:
Contact cerealito
Website

Duplicate symbol names in shared library

  • Quote

Post by cerealito » Mon Jun 04, 2007 9:36 am

Hi everyone; I have a Linux/C programming question, and although it is not gentoo-specific, I was hoping that you could shed some light into my life.

We have this strange prototype at work which is based upon an avionics simulator, written in C and running on Linux. The original simulator provides all the functionality of the avionics operating system and gives the avionics applications access to the system through an API. Each individual application is loaded in memory as a shared object using dlopen().

Now, the prototype, which adds certain features to the original simulator, is to be used as a testing platform of some software functions used by the aircraft. For the sake of the test, some operating system calls are to be controlled in a "deterministic" way, thus redefining these O.S. functions in the loaded application.

The problem with this is, that the shared-object of the application contains symbols with the same name and signature as some system functions in the simulator. In spite of my experiments using LD_PRELOAD and different flags on dlopen, I always get the simulator functions to execute instead of those in the shared object (the application). Because there are a couple hundreds of tests redefining different symbols each time, recompiling the simulator is not an option.

Here is an example of the problem:
main.c: the simulator code, it has the "real" OS system functions.

Code: Select all

#include <stdio.h>
#include <dlfcn.h>

void duplicate() {
	printf("This is the first (hard coded) function\n");
}

int main() {
	dlopen("appTest.so",RTLD_NOW|RTLD_LOCAL); //flags do not seem to change the behavior
	duplicate();
	return 0;
}
appTest.c: the application code, redefines a symbol:

Code: Select all

#include <stdio.h>
void duplicate() {
	printf("this is the Application dynamic library!!! :)\n");
}
compilation and results

Code: Select all

$ gcc -Wall -shared appTest.c -o appTest.so
$ gcc -Wall main.c -ldl
$ export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:.
$ export LD_PRELOAD=appTest.so
$ ./a.out
This is the first (hard coded) function
Why isn't LD_PRELOAD working? Am I missing something here? How can I force a program to execute the code on a loaded shared library instead of that in his own code? I supose that this is the normal, expected way of Linux to behave (being able to "overwrite" code being executed would have important security issues). I've red of a dlopen flag called DEEP_BIND in recent glibc versions, but I'm not sure if that's what I need and I wanted to ask here first.

Any explanation/help/suggestion will be greatly appreciated....

(I'm using gcc (GCC) 3.4.4 20050721 and Linux 2.6.9-22.ELsmp)

Cheers!
http://cerealito.free.fr
Top
ricce_n
Tux's lil' helper
Tux's lil' helper
User avatar
Posts: 130
Joined: Thu Jul 01, 2004 2:17 pm

  • Quote

Post by ricce_n » Mon Jun 04, 2007 12:01 pm

Take a handle from dlopen:

Code: Select all

int main(int argc, char **argv) {
void *handle;
void (*pduplicate)(void);
handle = dlopen("appTest.so",RTLD_NOW|RTLD_LOCAL);
pduplicate = dlsym(handle, "duplicate");
if(pduplicate) pduplicate();
else ;// the symbol `duplicate' dose not exist in appTest.so or is NULL.
}
Top
cerealito
n00b
n00b
User avatar
Posts: 20
Joined: Sat Apr 10, 2004 5:09 pm
Location: Toulouse, Fr
Contact:
Contact cerealito
Website

  • Quote

Post by cerealito » Mon Jun 04, 2007 1:31 pm

ricce_n, thanks a lot for your reply.
I had not thought about that before. Indeed, that works, and I am definitely considering it as an option now. However, I would like to continue searching for a "solution" that would not imply (big) modifications to the simulator code because the api is quite big in number of functions and I do not know which applications redefine the functions of the api (not all applications redefine the same ones, if at all).

thank you anyways, I've already learned something new!. If you have any other clue please let me know!
http://cerealito.free.fr
Top
didl
Retired Dev
Retired Dev
User avatar
Posts: 1106
Joined: Tue Sep 09, 2003 11:01 pm
Location: Pittsburgh, PA

  • Quote

Post by didl » Mon Jun 04, 2007 3:15 pm

I don't know your project at all and how mission critical it
is but I'd say that if you want it to be a robust application
then remove these duplicate symbols and don't rely on
the loader and other magic to find the proper symbols, since
otherwise your app will likely crash badly.
Even if it may be a lot of work to fix the code, in the long
run it will pay off big time and your application may actually
run.
Top
Gergan Penkov
Veteran
Veteran
User avatar
Posts: 1464
Joined: Sat Jul 17, 2004 9:42 pm
Location: das kleinste Kuhdorf Deutschlands :)

  • Quote

Post by Gergan Penkov » Mon Jun 04, 2007 7:21 pm

I don't think that LD_LIBRARY_PRELOAD could redefine the internal symbols, does the same happen, if you have the both definitions of these functions exported from shared objects and link your main application to one of them.
And as didl said, LD_LIBRARY_PRELOAD is an ugly hack for misbehaving apps or debuging and should not be used at all in production.
"I knew when an angel whispered into my ear,
You gotta get him away, yeah
Hey little bitch!
Be glad you finally walked away or you may have not lived another day."
Godsmack
Top
Post Reply

5 posts • Page 1 of 1

Return to “Portage & Programming”

Jump to
  • Assistance
  • ↳   News & Announcements
  • ↳   Frequently Asked Questions
  • ↳   Installing Gentoo
  • ↳   Multimedia
  • ↳   Desktop Environments
  • ↳   Networking & Security
  • ↳   Kernel & Hardware
  • ↳   Portage & Programming
  • ↳   Gamers & Players
  • ↳   Other Things Gentoo
  • ↳   Unsupported Software
  • Discussion & Documentation
  • ↳   Documentation, Tips & Tricks
  • ↳   Gentoo Chat
  • ↳   Gentoo Forums Feedback
  • ↳   Duplicate Threads
  • International Gentoo Users
  • ↳   中文 (Chinese)
  • ↳   Dutch
  • ↳   Finnish
  • ↳   French
  • ↳   Deutsches Forum (German)
  • ↳   Diskussionsforum
  • ↳   Deutsche Dokumentation
  • ↳   Greek
  • ↳   Forum italiano (Italian)
  • ↳   Forum di discussione italiano
  • ↳   Risorse italiane (documentazione e tools)
  • ↳   Polskie forum (Polish)
  • ↳   Instalacja i sprzęt
  • ↳   Polish OTW
  • ↳   Portuguese
  • ↳   Documentação, Ferramentas e Dicas
  • ↳   Russian
  • ↳   Scandinavian
  • ↳   Spanish
  • ↳   Other Languages
  • Architectures & Platforms
  • ↳   Gentoo on ARM
  • ↳   Gentoo on PPC
  • ↳   Gentoo on Sparc
  • ↳   Gentoo on Alternative Architectures
  • ↳   Gentoo on AMD64
  • ↳   Gentoo for Mac OS X (Portage for Mac OS X)
  • Board index
  • All times are UTC
  • Delete cookies

© 2001–2026 Gentoo Foundation, Inc.

Powered by phpBB® Forum Software © phpBB Limited

Privacy Policy

 

 

magic