- // If the program didn't explicitly call exit, call exit now, for the program.
- // This ensures that any atexit handlers get called correctly.
- Function *Exit = MP->getModule()->getOrInsertFunction("exit", Type::VoidTy,
- Type::IntTy,
- (Type *)0);
+ // Add the module's name to the start of the vector of arguments to main().
+ InputArgv.insert(InputArgv.begin(), InputFile);
+
+ // Call the main function from M as if its signature were:
+ // int main (int argc, char **argv, const char **envp)
+ // using the contents of Args to determine argc & argv, and the contents of
+ // EnvVars to determine envp.
+ //
+ Function *MainFn = Mod->getFunction("main");
+ if (!MainFn) {
+ std::cerr << "'main' function not found in module.\n";
+ return -1;
+ }
+
+ // If the program doesn't explicitly call exit, we will need the Exit
+ // function later on to make an explicit call, so get the function now.
+ Constant *Exit = Mod->getOrInsertFunction("exit", Type::VoidTy,
+ Type::Int32Ty, NULL);
+
+ // Reset errno to zero on entry to main.
+ errno = 0;
+
+ // Run static constructors.
+ EE->runStaticConstructorsDestructors(false);