r/programming • • Jul 25 '26

Writing a (valid) C program without main()

https://labs.iximiuz.com/tutorials/c-program-without-main-a1eea557
193 Upvotes

43 comments sorted by

View all comments

Show parent comment

18

u/yowhyyyy Jul 26 '26

Funnily enough this is something I’ve also played around with for my own reasons. You can align the stack as well manually if need be without passing crazy GCC flags.

I.e have you ever noticed GLIBC’s .so is actually executable? Ever wondered how they do that? Where’s main if it’s a .so? I’ll leave you with those amazing questions because it also lead me down a pretty cool rabbit hole.

2

u/Existing_Invite7129 Jul 28 '26

Take my upvote in exchange for a hint when you have some time.

2

u/yowhyyyy Aug 15 '26 edited Aug 15 '26

Sorry for the late response, didn’t get a notification for some reason. It’s how they compile it to my understanding! Passing a separate entry point option (-e) while still compiling as a shared object which is actually possible via GCC. It’s more or less the thought process of making a shared object executable. You also have to manually add an ELF interpreter attribute. That more or less is necessary for every process on Linux to execute if it’s dynamically compiled.

Normally when compiling a shared object it isn’t present as there is no execution/runtime info really needed. When you want it to actually execute though like a normal binary, you need to manually add the attribute.

TLDR: Manually specify an entry point, align stack depending on architecture, add ELF interpreter and go about with your regular routines. I’ve manually implemented this as stated above for playing around with shared objects that can execute for AIO concepts I had previously.

Edit: formatting and spelling