I was asked an interview question to change the entry point of a C or C++ program from main()
to any other function. How is it possible?
13 Answers
In standard C (and, I believe, C++ as well), you can't, at least not for a hosted environment (but see below). The standard specifies that the starting point for the C code is main
. The standard (c99) doesn't leave much scope for argument:
5.1.2.2.1 Program startup: (1) The function called at program startup is named main.
That's it. It then waffles on a bit about parameters and return values but there's really no leeway there for changing the name.
That's for a hosted environment. The standard also allows for a freestanding environment (i.e., no OS, for things like embedded systems). For a freestanding environment:
In a freestanding environment (in which C program execution may take place without any benefit of an operating system), the name and type of the function called at program startup are implementation-defined. Any library facilities available to a freestanding program, other than the minimal set required by clause 4, are implementation-defined.
You can use "trickery" in C implementations so that you can make it look like main
isn't the entry point. This is in fact what early Windows compliers did to mark WinMain
as the start point.
First way: a linker may include some pre-main startup code in a file like start.o
and it is this piece of code which runs to set up the C environment then call main
. There's nothing to stop you replacing that with something that calls bob
instead.
Second way: some linkers provide that very option with a command-line switch so that you can change it without recompiling the startup code.
Third way: you can link with this piece of code:
int main (int c, char *v[]) { return bob (c, v); }
and then your entry point for your code is seemingly bob
rather than main
.
However, all this, while of possibly academic interest, doesn't change the fact that I can't think of one single solitary situation in my many decades of cutting code, where this would be either necessary or desirable.
I would be asking the interviewer: why would you want to do this?
-
8+1 for making
main
just call something else. That's the most straightforward and cross-platform method to accomplish this.– SethCommented Oct 20, 2010 at 5:36 -
1Isn't it possible to do some implementation defined hackery with the constructors of global objects? Demons flying out your nose on other platforms guaranteed, of course. Commented Oct 20, 2010 at 8:47
-
-
I would have gone with "hack the startup code", which on windows is crt0.c or so. You mentioned that - hence the +1. As for reasons to do this... having an additional "Instrumented" entry point for debug purposes comes to mind. One which configures loggers etc. in a different way, without having to read configuration files etc. Your custom crt0 hack could then automagically deduce, which "main" version to call. Commented Jul 7 at 16:44
The entry point is actually the _start
function (implemented in crt1.o) .
The _start
function prepares the command line arguments and then calls main(int argc,char* argv[], char* env[])
,
you can change the entry point from _start
to mystart
by setting a linker parameter:
g++ file.o -Wl,-emystart -o runme
Of course, this is a replacement for the entry point _start
so you won't get the command line arguments:
void mystart(){
}
Note that global/static variables that have constructors or destructors must be initialized at the beginning of the application and destroyed at the end. Keep that in mind if you are planning on bypassing the default entry point which does it automatically.
From C++ standard docs 3.6.1 Main Function,
A program shall contain a global function called main, which is the designated start of the program. It is implementation-defined whether a program in a freestanding environment is required to define a main function.
So, it does depend on your compiler/linker...
-
1That's interesting. There's no indication in cpp0x that the first sentence there applies only to hosted, and
shall
is normally a standards word that means it must be so. So it appears thatmain
must exist even for freestanding. Can someone with more C++-Standard-fu than me figure that out? Can you have amain
without being required to define it?? Commented Oct 20, 2010 at 5:11 -
Actually that appears to be the case for C as well. I was looking at the hosted section. The freestanding section is a bit looser with the requirements. +1. Commented Oct 20, 2010 at 5:19
-
@paxdiablo: I think the intent is that the second sentence qualifies the first sentence, so that it means "A program [in a hosted environment] shall contain..." I think "shall contain" means "shall contain [a definition of];" I don't know how else the word "contain" could be understood. Commented Oct 20, 2010 at 5:21
-
@paxdiablo, I did refer from N2461 draft one.. Haven't updated to the recent though.. Am I making a mistake over here??– liaKCommented Oct 20, 2010 at 5:24
-
No, I don't think you're making a mistake here, liaK, there's similar language in C99. Commented Oct 20, 2010 at 5:25
If you are on VS2010, this could give you some idea
As it is easy to understand, this is not mandated by the C++ standard and falls in the domain of 'implemenation specific behavior'.
This is highly speculative, but you might have a static initializer instead of main:
#include <iostream>
int mymain()
{
std::cout << "mymain";
exit(0);
}
static int sRetVal = mymain();
int main()
{
std::cout << "never get here";
}
You might even make it 'Java-like', by putting the stuff in a constructor:
#include <iostream>
class MyApplication
{
public:
MyApplication()
{
std::cout << "mymain";
exit(0);
}
};
static MyApplication sMyApplication;
int main()
{
std::cout << "never get here";
}
Now. The interviewer might have thought about these, but I'd personally never use them. The reasons are:
- It's non-conventional. People won't understand it, it's nontrivial to find the entry point.
- Static initialization order is nondeterministic. Put in another static variable and you'll never now if it gets initialized.
That said, I've seen it being used in production instead of init()
for library initializers. The caveat is, on windows, (from experience) your statics in a DLL might or might not get initialized based on usage.
Modify the crt object that actually calls the main()
function, or provide your own (don't forget to disable linking of the normal one).
With gcc, declare the function with attribute((constructor)) and gcc will execute this function before any other code including main.
For Solaris Based Systems I have found this. You can use the .init
section for every platforms I guess:
pragma init (function [, function]...)
Source:
This pragma causes each listed function to be called during initialization (before main) or during shared module loading, by adding a call to the .init section.
It's very simple:
As you should know when you use constants in c, the compiler execute a kind of 'macro' changing the name of the constant for the respective value.
just include a #define
argument in the beginning of your code with the name of start-up function followed by the name main
:
Example:
#define my_start-up_function (main)
I think it is easy to remove the undesired main() symbol from the object before linking.
Unfortunately the entry point option for g++ is not working for me(the binary crashes before entering the entry point). So I strip undesired entry-point from object file.
Suppose we have two sources that contain entry point function.
- target.c contains the main() we do not want.
- our_code.c contains the testmain() we want to be the entry point.
After compiling(g++ -c option) we can get the following object files.
- target.o, that contains the main() we do not want.
- our_code.o that contains the testmain() we want to be the entry point.
So we can use the objcopy to strip undesired main() function.
objcopy --strip-symbol=main target.o
We can redefine testmain() to main() using objcopy too.
objcopy --redefine-sym testmain=main our_code.o
And then we can link both of them into binary.
g++ target.o our_code.o -o our_binary.bin
This works for me. Now when we run our_binary.bin
the entry point is our_code.o:main()
symbol which refers to our_code.c::testmain()
function.
On windows there is another (rather unorthodox) way to change the entry point of a program: TLS
. See this for more explanations: http://isc.sans.edu/diary.html?storyid=6655
Yes, We can change the main function name to any other name for eg. Start, bob, rem etc.
How does the compiler knows that it has to search for the main() in the entire code ?
Nothing is automatic in programming. somebody has done some work to make it looks automatic for us.
so it has been defined in the start up file that the compiler should search for main().
we can change the name main to anything else eg. Bob and then the compiler will be searching for Bob() only.
Changing a value in Linker Settings will override the entry point. i.e., MFC applications use a value of 'Windows (/SUBSYSTEM:WINDOWS)' to change entry point from main() to CWinApp::WinMain().
Right clicking on solution > Properties > Linker > System > Subsystem > Windows (/SUBSYSTEM:WINDOWS)
...
Very practical benefit to modifying entry point:
MFC is a framework we take advantage of to write Windows applications in C++. I know it's ancient, but my company maintains one for legacy reasons! You will not find a main() in MFC code. MSDN says the entry point is WinMain(), instead. Thus, you can override the WinMain() of your base CWinApp object. Or, most people override CWinApp::InitInstance() because the base WinMain() will call it.
Disclaimer: I use empty parentheses to denote a method, without caring how many arguments.
main
, or some implementation-defined entry point. There's no standard way.main
; instead, the entry point function does a few initialization tasks and then, for a C or C++ program, callsmain
. So the question does not make sense. Are you sure that that was the exact question?