this post was submitted on 28 May 2025
731 points (96.3% liked)
Programmer Humor
24369 readers
669 users here now
Welcome to Programmer Humor!
This is a place where you can post jokes, memes, humor, etc. related to programming!
For sharing awful code theres also Programming Horror.
Rules
- Keep content in english
- No advertisements
- Posts must be related to programming or programmer topics
founded 2 years ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
view the rest of the comments
Can someone explain to me how to compile a C library with "main" and a program with main? How does executing a program actually work? It has an executable flag, but what actually happens in the OS when it encounters a file with an executable file? How does it know to execute "main"? Is it possible to have a library that can be called and also executed like a program?
Anti Commercial-AI license
Way too long an answer for a lemmy post
Depends on OS. Linux will look at the first bytes of the file, either see (ASCII)
#!
(called a shebang) or ELF magic, then call the appropriate interpreter with the executable as an argument. When executing e.g. python, it's going to call/usr/bin/env
with parameterspython
and the file name because the shebang was#!/usr/bin/env python
.Compiled C programs are ELF so it will go through the ELF header, figure out which
ld.so
to use, then start that so that it will find all the libraries, resolve all dynamic symbols, then do some bookkeeping, and jump to_start
. That is, it doesn't:main
is a C thing.Absolutely.
ld.so
is an example of that.. Actually, wait, I'm not so sure any more, I'm getting things mixed up withlibdl.so
. In any caseld.so
is an executable with a file extension that makes it look like a library.EDIT: It does work. My (GNU) libc spits out version info when executed as an executable.
If you want to start looking at the innards like that I would suggest starting here: Hello world in assembly. Note the absence of a
main
function, the symbol the kernel actually invokes is_start
, the setup necessary to call a Cmain
is done bylibc.so
. Don't try to understand GNU's libc it's full of hystarical raisins I would suggest musl.How does that work? There must be something above
ld.so
, maybe the OS? Because looking at the ELF header,ld.so
is a shared library "Type: DYN (Shared object file)"The program headers don't have interpreter information either. Compare that to
ls
"Type: EXEC (Executable file)".It feels like somewhere in the flow there is the same thing that's happening in python just more hidden. Python seems to expose it because a file can be a library and an executable at the same time.
Anti Commercial-AI license
~~Your ld.so contains:~~
EDIT: ...with which I meant, modulo brainfart: My
libc.so.6
contains a proper entry address, while other libraries are pointing at0x0
and coredump when executed.libc.so
is a linker script, presumably because GNU compulsively overcomplicates everything....I guess that's enough for the kernel. It might be a linux-only thing, maybe even unintended and well linux doesn't break userspace.
Speaking of, I was playing it a bit fast and loose:
_start
is merely the default symbol name for the entry label, I'm sure nasm and/or ld have ways to set it to something different.Btw,
ld.so
is a symlink told-linux-x86-64.so.2
at least on my system. It is an statically linked executable. Theld.so
is, in simpler words, an interpreter for the ELF format and you can run it:Which seems to be contained in the only executable ~~section~~ segment of
ld.so
Edit: My understanding of this quite shallow; the above is a segment that in this case contains the entirety of the
.text
section.You don't. In C everything gets referenced by a symbol during the link stage of compilation. Libraries ultimately get treated like your source code during compilation and all items land in a symbol table. Two items with the same name result in a link failure and compilation aborts. So a library and a program with main is no bueno.
When Linux loads an executable they basically look at the program's symbol table and search for "main" then start executing at that point
Windows behaves mostly the same way, as does MacOS. Most RTOS's have their own special way of doing things, bare metal you're at the mercy of your CPU vendor. The C standard specifies that "main" is the special symbol we all just happen to use
If you want to have a library that can also be a standalone executable, just put the main function in an extra file and don't compile that file when using the library as a library.
You could also use the preprocessor to do it similar to python but please don't.
Just use any build tool, and have two targets, one library and one executable:
Edit: added example
I haven't done much low level stuff, but I think the 'main' function is something the compiler uses to establish an entry point for the compiled binary. The name 'main' would not exist in the compiled binary at all, but the function itself would still exist. Executable formats aren't all the same, so they'll have different ways of determining where this entry point function is expected to be. You can 'run' a binary library file by invoking a function contained therein, which is how DLL files work.
There are a lot of other helpful replies in this thread, so I won't add much, but I did find this reference, which you could read if you have a lot of free time. But I particularly liked reading this summary: