LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-04-2009, 09:19 AM   #1
JazzItSelf
Member
 
Registered: Oct 2003
Location: Boston
Distribution: Slackware64-current
Posts: 44

Rep: Reputation: 18
how to create an executable shared library (like libc-x.x.so or ld-x.x.so)


Hi all-

I'm writing a shared library and I'd like to have it be executable like the libc or ld libraries such that it prints some version info when executed. I found an example (referenced at the end of this post) but I've been unable to make it work correctly. I did update the string with the correct path to ld.

From what I can gather, there are two things that I need to do for the library to be executable:
  1. define an entry point
  2. reference an interpreter in the .interp section

but I'm obviously missing something critical here because the simplist example does exactly this and fails.

The referenced example compiles without error, but will not execute
Code:
$ lsl dop.so
-rwxr-xr-x 1 cparker users 7.9K 2009-11-04 09:06 dop.so
$ ./dop.so
bash: ./dop.so: No such file or directory
$ /lib64/ld-2.9.so dop.so
dop.so: error while loading shared libraries: dop.so: cannot open shared object file


when I'm working with my own library, I just get segmentation faults
Code:
const char my_interp[] __attribute__((section(".interp"))) = "/lib64/ld-linux-x86_64.so.2";
...
...
// entry point to display the library information if executed
// extern "C" required to prevent the symbol from being mangled
extern "C" void lcipacket_main (int argc, char * argv[]) {
	printf("liblcipacket version %d.%d.%d\n", MAJOR_VERSION, MINOR_VERSION,
						  REVISION);
} // function main


compiling and executing results in:
Code:
$ make
gcc -fPIC -Wall -g -c liblcipacket.cpp
gcc -lc -shared -Wl,-soname,liblcipacket.so.0,-e,lcipacket_main -o liblcipacket.so.0.0.0 liblcipacket.o
$ ./liblcipacket.so.0
Segmentation fault


Can anyone offer some wisdom?
Thanks,
--Chad

for reference:
gcc 4.3.3
ld/libc 2.9
arch x86_64

from http://gcc.gnu.org/ml/gcc-help/2003-07/msg00232.html

Makefile:
Code:
dop.so: dop.c
	gcc -Wall -W -fPIC -shared -o dop.so dop.c -lc -Wl,-e,my_main

Source file (dop.c):
Code:

const char my_interp[] __attribute__((section(".interp"))) = "/lib/ld.so.1";

void my_main(int argc, char **argv)
{
	int i;

	printf("Called as:");
	for (i = 0; i < argc; i++)
		printf(" %s", argv[i]);
	printf("\n");

	exit(0);
}
 
Old 11-05-2009, 06:40 PM   #2
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
Why are you trying to do this? If this is just to play around with ELF, that's fine, but I can't think of a good reason to do this seriously.. especially since I actually care about cross-platform compatibility.
 
Old 11-07-2009, 07:47 AM   #3
JazzItSelf
Member
 
Registered: Oct 2003
Location: Boston
Distribution: Slackware64-current
Posts: 44

Original Poster
Rep: Reputation: 18
no real reason in particular... I just thought it would be fun to try. Of course now the problem is that it's a problem... and as such must be solved!
--Cp
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Linking an executable to a shared library (C++) lennyk Programming 4 06-25-2009 04:45 AM
Changes in libc Shared Library ggulati2 Linux - General 1 05-03-2009 07:17 PM
How can I create a Shared library? Reina Programming 1 08-12-2007 11:14 PM
kmodule: error while loading shared library libc.so.6 gruell Fedora - Installation 2 08-04-2005 09:24 AM
Trying to create a shared library mukund_desh Linux - Software 1 05-24-2005 05:41 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:17 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration