0

I've been trying to add an entry point to a C shared library, so that executing

$ ./lib/mylib.so

prints some helpful information to the terminal.

Following the work here, I was able to get my library to run like so:

$ /lib64/ld-linux-x86-64.so.2 ./lib/mylib.so
Some useful configuration information about this library:
- blah blah
- Gcc version: gcc-9
- Hostname where compiled: foo

$

However, when I run without the interpreter, I get a no such file or directory error. Why is this? What can I do to help my shell find the correct interpreter?

ijustlovemath
  • 703
  • 10
  • 21
  • 2
    Re "*What can I do to help my shell find the correct interpreter?*" Shell doesn't do anything but pass the file name to the OS. It's the OS that determines how to run it. On Linux, it's either a binary executable (no interpreter), or a file that starts with a shebang (`#!`) line identifying the interpreter. – ikegami Dec 16 '22 at 20:52
  • Why? what is a point of executing a shared lib? just create a normal 'c' executable instead. Any way, in order to start a program, system needs an entry point in it. In 'c' it is the 'main' function. – Serge Dec 16 '22 at 20:56
  • In Linux, both executables and libraries are ELF files. I imagine you could simply make an executable, but use the naming convention of a library? Don't forget to turn on the "x" permission on the file. – ikegami Dec 16 '22 at 20:58
  • @Serge, I think `main` isn't the actual the entry point. There's an internal function that serves as the entry point. It calls `main` after initializing things (and doing stuff like getting the command line and parsing it on Windows). – ikegami Dec 16 '22 at 20:59
  • Wait, did you simply forget to set the "x" flag using chmod? – ikegami Dec 16 '22 at 21:03
  • Have you followed the links from your x-ref to [Executing a shared library](https://unix.stackexchange.com/q/7066/5131) on Unix & Linux back to SO and [Building a `.so` that is also an executable](https://stackoverflow.com/q/1449987/15168)? There seems to be a good modern recipe there in the [answer](https://stackoverflow.com/a/68339111/15168) by [Andrew G Morgan](https://stackoverflow.com/users/14760867/andrew-g-morgan). – Jonathan Leffler Dec 16 '22 at 21:09
  • @ikegami I meant that within 'c' code (which OP claims the so library is written) 'main' is the standard abstract name that starts execution of a 'c' program. Real implementation of the loader depends on the system and 'c' compiler. But in order to have a self-executed code one must provide such an entry as minimum. But in any case the question is: why bother? – Serge Dec 17 '22 at 03:17
  • My problem was that I had mistyped the .interp string. Fixing that led it to behave as expected. As to why? It's very useful for a distributed library to be able to report information about its configuration at compile time, to help aid debugging once deployed. I also thought it was chmod, but that wasn't the issue. Running it in strace and looking up the error ENOENT on the execve manpage led me to check the interpreter again. – ijustlovemath Dec 17 '22 at 15:33

0 Answers0