15

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.

Marco Bonelli
  • 63,369
  • 21
  • 118
  • 128
franziskus
  • 361
  • 2
  • 7
  • 4
    See also http://stackoverflow.com/questions/1449987/building-a-so-that-is-also-an-executable – Brian L Feb 10 '11 at 22:13
  • 1
    Does this answer your question? [building a .so that is also an executable](https://stackoverflow.com/questions/1449987/building-a-so-that-is-also-an-executable) – AdminBee Jul 13 '21 at 07:20

1 Answers1

10

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
Marco Bonelli
  • 63,369
  • 21
  • 118
  • 128
vpit3833
  • 7,817
  • 2
  • 25
  • 25