Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Executing a shared library on Unix

Some Unix shared libraries provide an output when called from the command line as if they were executables. For example:

$ /lib/libc.so.6 
GNU C Library stable release version 2.13, by Roland McGrath et al.
Copyright (C) 2011 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.
There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.
Compiled by GNU CC version 4.5.2.
Compiled on a Linux 2.6.37 system on 2011-01-18.
[...]

In a shared library of my own written in C, how can I provide this output? I've executed now a library I just made and I get a segment fault.

Note: I asked this previously on Unix & Linux SE here.

like image 833
franziskus Avatar asked Feb 10 '11 21:02

franziskus


People also ask

How do I open a shared library in Linux?

You need to run the ldconfig command to effect the changes. After creating your shared library, you need to install it. You can either move it into any of the standard directories mentioned above and run the ldconfig command.

What are shared libraries in Unix?

Shared libraries are programs that, when loaded, are put in the shared library region for system-wide sharing. A program is loaded as a shared library program if the executable file has the shared library extended attribute set.

How do I load a shared library?

A process can load a shared library at runtime by using the dlopen() call, which instructs the runtime linker to load this library. Once the library is loaded, the program can call any function within that library by using the dlsym() call to determine its address.


1 Answers

The below definition of main is responsible for printing the output you see. It is defined in csu/version.c of the source tree of glibc. I hope this helps.

#ifdef HAVE_ELF
/* This function is the entry point for the shared object.
   Running the library as a program will get here.  */

extern void __libc_main (void) __attribute__ ((noreturn));
void
__libc_main (void)
{
  __libc_print_version ();
  _exit (0);
}
#endif
like image 159
vpit3833 Avatar answered Oct 02 '22 15:10

vpit3833