2003-09-30 03:24:28 +00:00
|
|
|
//===- GenerateCode.cpp - Functions for generating executable files ------===//
|
2005-04-22 00:00:37 +00:00
|
|
|
//
|
2003-10-20 17:47:21 +00:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by the LLVM research group and is distributed under
|
|
|
|
// the University of Illinois Open Source License. See LICENSE.TXT for details.
|
2005-04-22 00:00:37 +00:00
|
|
|
//
|
2003-10-20 17:47:21 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2003-09-18 16:22:26 +00:00
|
|
|
//
|
|
|
|
// This file contains functions for generating executable files once linking
|
|
|
|
// has finished. This includes generating a shell script to run the JIT or
|
|
|
|
// a native executable derived from the bytecode.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2003-09-30 17:33:12 +00:00
|
|
|
#include "gccld.h"
|
2004-12-14 04:20:08 +00:00
|
|
|
#include "llvm/System/Program.h"
|
2003-09-18 16:22:26 +00:00
|
|
|
#include "llvm/Module.h"
|
|
|
|
#include "llvm/PassManager.h"
|
2003-11-09 19:55:09 +00:00
|
|
|
#include "llvm/Analysis/LoadValueNumbering.h"
|
2004-07-27 08:13:15 +00:00
|
|
|
#include "llvm/Analysis/Passes.h"
|
2003-11-16 23:07:28 +00:00
|
|
|
#include "llvm/Analysis/Verifier.h"
|
2005-02-28 08:45:35 +00:00
|
|
|
#include "llvm/Bytecode/Archive.h"
|
2003-09-18 16:22:26 +00:00
|
|
|
#include "llvm/Bytecode/WriteBytecodePass.h"
|
2003-09-30 17:33:12 +00:00
|
|
|
#include "llvm/Target/TargetData.h"
|
|
|
|
#include "llvm/Transforms/IPO.h"
|
|
|
|
#include "llvm/Transforms/Scalar.h"
|
2004-09-01 22:55:40 +00:00
|
|
|
#include "llvm/Support/SystemUtils.h"
|
|
|
|
#include "llvm/Support/CommandLine.h"
|
2004-12-14 04:20:08 +00:00
|
|
|
|
2003-11-11 22:41:34 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
2003-10-24 18:09:23 +00:00
|
|
|
namespace {
|
|
|
|
cl::opt<bool>
|
|
|
|
DisableInline("disable-inlining", cl::desc("Do not run the inliner pass"));
|
2003-11-16 23:07:28 +00:00
|
|
|
|
|
|
|
cl::opt<bool>
|
|
|
|
Verify("verify", cl::desc("Verify intermediate results of all passes"));
|
|
|
|
|
|
|
|
cl::opt<bool>
|
|
|
|
DisableOptimizations("disable-opt",
|
|
|
|
cl::desc("Do not run any optimization passes"));
|
2003-10-24 18:09:23 +00:00
|
|
|
}
|
|
|
|
|
2004-06-02 00:22:24 +00:00
|
|
|
/// CopyEnv - This function takes an array of environment variables and makes a
|
|
|
|
/// copy of it. This copy can then be manipulated any way the caller likes
|
|
|
|
/// without affecting the process's real environment.
|
|
|
|
///
|
|
|
|
/// Inputs:
|
|
|
|
/// envp - An array of C strings containing an environment.
|
|
|
|
///
|
|
|
|
/// Return value:
|
|
|
|
/// NULL - An error occurred.
|
|
|
|
///
|
|
|
|
/// Otherwise, a pointer to a new array of C strings is returned. Every string
|
|
|
|
/// in the array is a duplicate of the one in the original array (i.e. we do
|
|
|
|
/// not copy the char *'s from one array to another).
|
|
|
|
///
|
|
|
|
static char ** CopyEnv(char ** const envp) {
|
|
|
|
// Count the number of entries in the old list;
|
|
|
|
unsigned entries; // The number of entries in the old environment list
|
|
|
|
for (entries = 0; envp[entries] != NULL; entries++)
|
|
|
|
/*empty*/;
|
|
|
|
|
|
|
|
// Add one more entry for the NULL pointer that ends the list.
|
|
|
|
++entries;
|
|
|
|
|
|
|
|
// If there are no entries at all, just return NULL.
|
|
|
|
if (entries == 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
// Allocate a new environment list.
|
|
|
|
char **newenv = new char* [entries];
|
|
|
|
if ((newenv = new char* [entries]) == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
// Make a copy of the list. Don't forget the NULL that ends the list.
|
|
|
|
entries = 0;
|
|
|
|
while (envp[entries] != NULL) {
|
|
|
|
newenv[entries] = new char[strlen (envp[entries]) + 1];
|
|
|
|
strcpy (newenv[entries], envp[entries]);
|
|
|
|
++entries;
|
|
|
|
}
|
|
|
|
newenv[entries] = NULL;
|
|
|
|
|
|
|
|
return newenv;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// RemoveEnv - Remove the specified environment variable from the environment
|
|
|
|
/// array.
|
|
|
|
///
|
|
|
|
/// Inputs:
|
|
|
|
/// name - The name of the variable to remove. It cannot be NULL.
|
|
|
|
/// envp - The array of environment variables. It cannot be NULL.
|
|
|
|
///
|
|
|
|
/// Notes:
|
|
|
|
/// This is mainly done because functions to remove items from the environment
|
|
|
|
/// are not available across all platforms. In particular, Solaris does not
|
|
|
|
/// seem to have an unsetenv() function or a setenv() function (or they are
|
|
|
|
/// undocumented if they do exist).
|
|
|
|
///
|
|
|
|
static void RemoveEnv(const char * name, char ** const envp) {
|
|
|
|
for (unsigned index=0; envp[index] != NULL; index++) {
|
|
|
|
// Find the first equals sign in the array and make it an EOS character.
|
|
|
|
char *p = strchr (envp[index], '=');
|
|
|
|
if (p == NULL)
|
|
|
|
continue;
|
|
|
|
else
|
|
|
|
*p = '\0';
|
|
|
|
|
|
|
|
// Compare the two strings. If they are equal, zap this string.
|
|
|
|
// Otherwise, restore it.
|
|
|
|
if (!strcmp(name, envp[index]))
|
|
|
|
*envp[index] = '\0';
|
|
|
|
else
|
|
|
|
*p = '=';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-04-20 03:22:18 +00:00
|
|
|
static void dumpArgs(const char **args) {
|
2005-09-23 06:05:46 +00:00
|
|
|
std::cerr << *args++;
|
2005-04-20 03:22:18 +00:00
|
|
|
while (*args)
|
2005-09-23 06:05:46 +00:00
|
|
|
std::cerr << ' ' << *args++;
|
|
|
|
std::cerr << '\n' << std::flush;
|
2005-04-20 03:22:18 +00:00
|
|
|
}
|
|
|
|
|
2003-11-16 23:07:28 +00:00
|
|
|
static inline void addPass(PassManager &PM, Pass *P) {
|
|
|
|
// Add the pass to the pass manager...
|
|
|
|
PM.add(P);
|
2005-04-20 04:08:35 +00:00
|
|
|
|
2003-11-16 23:07:28 +00:00
|
|
|
// If we are verifying all of the intermediate steps, add the verifier...
|
|
|
|
if (Verify) PM.add(createVerifierPass());
|
|
|
|
}
|
|
|
|
|
2005-02-28 08:45:35 +00:00
|
|
|
static bool isBytecodeLibrary(const sys::Path &FullPath) {
|
|
|
|
// Check for a bytecode file
|
|
|
|
if (FullPath.isBytecodeFile()) return true;
|
|
|
|
// Check for a dynamic library file
|
|
|
|
if (FullPath.isDynamicLibrary()) return false;
|
|
|
|
// Check for a true bytecode archive file
|
|
|
|
if (FullPath.isArchive() ) {
|
|
|
|
std::string ErrorMessage;
|
|
|
|
Archive* ar = Archive::OpenAndLoadSymbols( FullPath, &ErrorMessage );
|
2005-04-20 04:08:35 +00:00
|
|
|
return ar->isBytecodeArchive();
|
2005-02-28 08:45:35 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2005-04-20 04:08:35 +00:00
|
|
|
static bool isBytecodeLPath(const std::string &LibPath) {
|
2005-07-07 23:21:43 +00:00
|
|
|
sys::Path LPath(LibPath);
|
|
|
|
|
2005-09-23 06:11:24 +00:00
|
|
|
// Make sure it exists and is a directory
|
2005-09-23 06:05:46 +00:00
|
|
|
try {
|
2005-09-23 06:11:24 +00:00
|
|
|
if (!LPath.exists() || !LPath.isDirectory())
|
|
|
|
return false;
|
2005-09-23 06:05:46 +00:00
|
|
|
} catch (std::string& xcptn) {
|
2005-09-23 06:11:24 +00:00
|
|
|
return false;
|
2005-07-08 16:48:52 +00:00
|
|
|
}
|
2005-09-23 06:11:24 +00:00
|
|
|
|
2005-02-28 08:45:35 +00:00
|
|
|
// Grab the contents of the -L path
|
|
|
|
std::set<sys::Path> Files;
|
|
|
|
LPath.getDirectoryContents(Files);
|
2005-09-23 06:11:24 +00:00
|
|
|
|
2005-02-28 08:45:35 +00:00
|
|
|
// Iterate over the contents one by one to determine
|
|
|
|
// if this -L path has any bytecode shared libraries
|
|
|
|
// or archives
|
|
|
|
std::set<sys::Path>::iterator File = Files.begin();
|
2005-09-23 06:11:24 +00:00
|
|
|
std::string dllsuffix = sys::Path::GetDLLSuffix();
|
2005-02-28 08:45:35 +00:00
|
|
|
for (; File != Files.end(); ++File) {
|
|
|
|
|
|
|
|
if ( File->isDirectory() )
|
|
|
|
continue;
|
2005-04-20 04:08:35 +00:00
|
|
|
|
2005-02-28 08:45:35 +00:00
|
|
|
std::string path = File->toString();
|
|
|
|
|
|
|
|
// Check for an ending '.dll,.so' or '.a' suffix as all
|
|
|
|
// other files are not of interest to us here
|
2005-09-23 06:11:24 +00:00
|
|
|
if (path.find(dllsuffix, path.size()-dllsuffix.size()) == std::string::npos
|
|
|
|
&& path.find(".a", path.size()-2) == std::string::npos)
|
2005-02-28 08:45:35 +00:00
|
|
|
continue;
|
2005-04-20 04:08:35 +00:00
|
|
|
|
2005-02-28 08:45:35 +00:00
|
|
|
// Finally, check to see if the file is a true bytecode file
|
|
|
|
if (isBytecodeLibrary(*File))
|
2005-09-23 06:11:24 +00:00
|
|
|
return true;
|
2005-02-28 08:45:35 +00:00
|
|
|
}
|
2005-09-23 06:11:24 +00:00
|
|
|
return false;
|
2005-02-28 08:45:35 +00:00
|
|
|
}
|
|
|
|
|
2003-09-30 17:42:57 +00:00
|
|
|
/// GenerateBytecode - generates a bytecode file from the specified module.
|
|
|
|
///
|
|
|
|
/// Inputs:
|
|
|
|
/// M - The module for which bytecode should be generated.
|
2004-12-02 21:26:10 +00:00
|
|
|
/// StripLevel - 2 if we should strip all symbols, 1 if we should strip
|
|
|
|
/// debug info.
|
2003-09-30 17:42:57 +00:00
|
|
|
/// Internalize - Flags whether all symbols should be marked internal.
|
|
|
|
/// Out - Pointer to file stream to which to write the output.
|
|
|
|
///
|
|
|
|
/// Returns non-zero value on error.
|
|
|
|
///
|
2004-12-02 21:26:10 +00:00
|
|
|
int llvm::GenerateBytecode(Module *M, int StripLevel, bool Internalize,
|
2004-04-06 16:54:04 +00:00
|
|
|
std::ostream *Out) {
|
2003-09-18 16:22:26 +00:00
|
|
|
// In addition to just linking the input from GCC, we also want to spiff it up
|
|
|
|
// a little bit. Do this now.
|
|
|
|
PassManager Passes;
|
|
|
|
|
2003-11-16 23:07:28 +00:00
|
|
|
if (Verify) Passes.add(createVerifierPass());
|
|
|
|
|
2003-09-18 16:22:26 +00:00
|
|
|
// Add an appropriate TargetData instance for this module...
|
2003-11-20 06:26:15 +00:00
|
|
|
addPass(Passes, new TargetData("gccld", M));
|
2003-11-16 23:07:28 +00:00
|
|
|
|
2003-11-28 09:44:03 +00:00
|
|
|
// Often if the programmer does not specify proper prototypes for the
|
|
|
|
// functions they are calling, they end up calling a vararg version of the
|
|
|
|
// function that does not get a body filled in (the real function has typed
|
|
|
|
// arguments). This pass merges the two functions.
|
|
|
|
addPass(Passes, createFunctionResolvingPass());
|
|
|
|
|
2004-11-16 18:59:20 +00:00
|
|
|
if (!DisableOptimizations) {
|
2005-10-18 06:29:43 +00:00
|
|
|
// Now that composite has been compiled, scan through the module, looking
|
|
|
|
// for a main function. If main is defined, mark all other functions
|
|
|
|
// internal.
|
|
|
|
addPass(Passes, createInternalizePass(Internalize));
|
2004-11-17 16:41:19 +00:00
|
|
|
|
2004-10-07 04:12:02 +00:00
|
|
|
// Now that we internalized some globals, see if we can hack on them!
|
|
|
|
addPass(Passes, createGlobalOptimizerPass());
|
2004-02-25 21:35:13 +00:00
|
|
|
|
2003-11-16 23:07:28 +00:00
|
|
|
// Linking modules together can lead to duplicated global constants, only
|
|
|
|
// keep one copy of each constant...
|
2003-11-20 06:26:15 +00:00
|
|
|
addPass(Passes, createConstantMergePass());
|
2003-11-16 23:07:28 +00:00
|
|
|
|
|
|
|
// Propagate constants at call sites into the functions they call.
|
2004-12-10 08:03:43 +00:00
|
|
|
addPass(Passes, createIPSCCPPass());
|
2003-10-24 18:09:23 +00:00
|
|
|
|
2003-11-16 23:07:28 +00:00
|
|
|
// Remove unused arguments from functions...
|
2003-11-20 06:26:15 +00:00
|
|
|
addPass(Passes, createDeadArgEliminationPass());
|
2003-11-09 19:55:09 +00:00
|
|
|
|
2003-11-16 23:07:28 +00:00
|
|
|
if (!DisableInline)
|
2003-11-20 06:26:15 +00:00
|
|
|
addPass(Passes, createFunctionInliningPass()); // Inline small functions
|
2003-11-09 19:55:09 +00:00
|
|
|
|
2004-04-12 05:38:15 +00:00
|
|
|
addPass(Passes, createPruneEHPass()); // Remove dead EH info
|
2004-10-11 04:47:18 +00:00
|
|
|
addPass(Passes, createGlobalOptimizerPass()); // Optimize globals again.
|
2004-04-12 05:38:15 +00:00
|
|
|
addPass(Passes, createGlobalDCEPass()); // Remove dead functions
|
|
|
|
|
2004-03-07 22:12:40 +00:00
|
|
|
// If we didn't decide to inline a function, check to see if we can
|
|
|
|
// transform it to pass arguments by value instead of by reference.
|
|
|
|
addPass(Passes, createArgumentPromotionPass());
|
|
|
|
|
2004-02-26 03:34:30 +00:00
|
|
|
// The IPO passes may leave cruft around. Clean up after them.
|
|
|
|
addPass(Passes, createInstructionCombiningPass());
|
|
|
|
|
|
|
|
addPass(Passes, createScalarReplAggregatesPass()); // Break up allocas
|
|
|
|
|
2003-11-16 23:07:28 +00:00
|
|
|
// Run a few AA driven optimizations here and now, to cleanup the code.
|
2004-08-02 10:10:08 +00:00
|
|
|
addPass(Passes, createGlobalsModRefPass()); // IP alias analysis
|
2003-09-18 16:22:26 +00:00
|
|
|
|
2003-11-20 06:26:15 +00:00
|
|
|
addPass(Passes, createLICMPass()); // Hoist loop invariants
|
|
|
|
addPass(Passes, createLoadValueNumberingPass()); // GVN for load instrs
|
|
|
|
addPass(Passes, createGCSEPass()); // Remove common subexprs
|
2004-07-27 08:13:15 +00:00
|
|
|
addPass(Passes, createDeadStoreEliminationPass()); // Nuke dead stores
|
2003-09-18 16:22:26 +00:00
|
|
|
|
2004-02-26 03:34:30 +00:00
|
|
|
// Cleanup and simplify the code after the scalar optimizations.
|
2003-11-20 06:26:15 +00:00
|
|
|
addPass(Passes, createInstructionCombiningPass());
|
2003-11-16 23:07:28 +00:00
|
|
|
|
|
|
|
// Delete basic blocks, which optimization passes may have killed...
|
2003-11-20 06:26:15 +00:00
|
|
|
addPass(Passes, createCFGSimplificationPass());
|
2003-11-16 23:07:28 +00:00
|
|
|
|
|
|
|
// Now that we have optimized the program, discard unreachable functions...
|
2003-11-20 06:26:15 +00:00
|
|
|
addPass(Passes, createGlobalDCEPass());
|
2003-11-16 23:07:28 +00:00
|
|
|
}
|
2003-09-18 16:22:26 +00:00
|
|
|
|
2004-12-02 21:26:10 +00:00
|
|
|
// If the -s or -S command line options were specified, strip the symbols out
|
|
|
|
// of the resulting program to make it smaller. -s and -S are GLD options
|
|
|
|
// that we are supporting.
|
|
|
|
if (StripLevel)
|
|
|
|
addPass(Passes, createStripSymbolsPass(StripLevel == 1));
|
2004-11-16 18:59:20 +00:00
|
|
|
|
2004-01-14 03:39:46 +00:00
|
|
|
// Make sure everything is still good.
|
|
|
|
Passes.add(createVerifierPass());
|
|
|
|
|
2003-09-18 16:22:26 +00:00
|
|
|
// Add the pass that writes bytecode to the output file...
|
2003-11-20 06:26:15 +00:00
|
|
|
addPass(Passes, new WriteBytecodePass(Out));
|
2003-09-18 16:22:26 +00:00
|
|
|
|
|
|
|
// Run our queue of passes all at once now, efficiently.
|
|
|
|
Passes.run(*M);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2003-09-30 17:42:57 +00:00
|
|
|
/// GenerateAssembly - generates a native assembly language source file from the
|
|
|
|
/// specified bytecode file.
|
|
|
|
///
|
|
|
|
/// Inputs:
|
|
|
|
/// InputFilename - The name of the output bytecode file.
|
|
|
|
/// OutputFilename - The name of the file to generate.
|
|
|
|
/// llc - The pathname to use for LLC.
|
|
|
|
///
|
|
|
|
/// Return non-zero value on error.
|
|
|
|
///
|
2004-04-06 16:54:04 +00:00
|
|
|
int llvm::GenerateAssembly(const std::string &OutputFilename,
|
|
|
|
const std::string &InputFilename,
|
2005-04-20 03:22:18 +00:00
|
|
|
const sys::Path &llc,
|
|
|
|
bool Verbose) {
|
2003-09-18 16:22:26 +00:00
|
|
|
// Run LLC to convert the bytecode file into assembly code.
|
2004-12-19 18:00:56 +00:00
|
|
|
std::vector<const char*> args;
|
2005-04-10 20:59:38 +00:00
|
|
|
args.push_back(llc.c_str());
|
2004-12-14 04:20:08 +00:00
|
|
|
args.push_back("-f");
|
|
|
|
args.push_back("-o");
|
2004-12-19 18:00:56 +00:00
|
|
|
args.push_back(OutputFilename.c_str());
|
|
|
|
args.push_back(InputFilename.c_str());
|
2005-02-13 23:02:34 +00:00
|
|
|
args.push_back(0);
|
2005-04-20 03:22:18 +00:00
|
|
|
if (Verbose) dumpArgs(&args[0]);
|
2004-12-19 18:00:56 +00:00
|
|
|
return sys::Program::ExecuteAndWait(llc, &args[0]);
|
2003-09-18 16:22:26 +00:00
|
|
|
}
|
|
|
|
|
2005-08-02 22:07:38 +00:00
|
|
|
/// GenerateCFile - generates a C source file from the specified bytecode file.
|
2004-04-06 16:54:04 +00:00
|
|
|
int llvm::GenerateCFile(const std::string &OutputFile,
|
|
|
|
const std::string &InputFile,
|
2005-04-20 03:22:18 +00:00
|
|
|
const sys::Path &llc,
|
|
|
|
bool Verbose) {
|
2004-04-06 16:43:13 +00:00
|
|
|
// Run LLC to convert the bytecode file into C.
|
2004-12-19 18:00:56 +00:00
|
|
|
std::vector<const char*> args;
|
2005-04-10 20:59:38 +00:00
|
|
|
args.push_back(llc.c_str());
|
2004-12-14 04:20:08 +00:00
|
|
|
args.push_back("-march=c");
|
|
|
|
args.push_back("-f");
|
|
|
|
args.push_back("-o");
|
2004-12-19 18:00:56 +00:00
|
|
|
args.push_back(OutputFile.c_str());
|
|
|
|
args.push_back(InputFile.c_str());
|
2005-02-13 23:02:34 +00:00
|
|
|
args.push_back(0);
|
2005-04-20 03:22:18 +00:00
|
|
|
if (Verbose) dumpArgs(&args[0]);
|
2004-12-19 18:00:56 +00:00
|
|
|
return sys::Program::ExecuteAndWait(llc, &args[0]);
|
2004-04-06 16:43:13 +00:00
|
|
|
}
|
|
|
|
|
2005-08-02 22:07:38 +00:00
|
|
|
/// GenerateNative - generates a native executable file from the specified
|
|
|
|
/// assembly source file.
|
2003-09-30 17:42:57 +00:00
|
|
|
///
|
|
|
|
/// Inputs:
|
|
|
|
/// InputFilename - The name of the output bytecode file.
|
|
|
|
/// OutputFilename - The name of the file to generate.
|
|
|
|
/// Libraries - The list of libraries with which to link.
|
|
|
|
/// gcc - The pathname to use for GGC.
|
|
|
|
/// envp - A copy of the process's current environment.
|
|
|
|
///
|
|
|
|
/// Outputs:
|
|
|
|
/// None.
|
|
|
|
///
|
|
|
|
/// Returns non-zero value on error.
|
|
|
|
///
|
2004-04-06 16:54:04 +00:00
|
|
|
int llvm::GenerateNative(const std::string &OutputFilename,
|
|
|
|
const std::string &InputFilename,
|
2005-02-28 08:45:35 +00:00
|
|
|
const std::vector<std::string> &LibPaths,
|
2004-04-06 16:54:04 +00:00
|
|
|
const std::vector<std::string> &Libraries,
|
2005-02-28 08:45:35 +00:00
|
|
|
const sys::Path &gcc, char ** const envp,
|
|
|
|
bool Shared,
|
2005-08-02 22:07:38 +00:00
|
|
|
bool ExportAllAsDynamic,
|
2005-02-28 08:45:35 +00:00
|
|
|
const std::string &RPath,
|
2005-04-20 03:22:18 +00:00
|
|
|
const std::string &SOName,
|
|
|
|
bool Verbose) {
|
2003-09-18 16:22:26 +00:00
|
|
|
// Remove these environment variables from the environment of the
|
|
|
|
// programs that we will execute. It appears that GCC sets these
|
|
|
|
// environment variables so that the programs it uses can configure
|
|
|
|
// themselves identically.
|
|
|
|
//
|
2003-09-30 17:33:12 +00:00
|
|
|
// However, when we invoke GCC below, we want it to use its normal
|
|
|
|
// configuration. Hence, we must sanitize its environment.
|
|
|
|
char ** clean_env = CopyEnv(envp);
|
2003-09-18 16:22:26 +00:00
|
|
|
if (clean_env == NULL)
|
|
|
|
return 1;
|
2003-09-30 17:33:12 +00:00
|
|
|
RemoveEnv("LIBRARY_PATH", clean_env);
|
|
|
|
RemoveEnv("COLLECT_GCC_OPTIONS", clean_env);
|
|
|
|
RemoveEnv("GCC_EXEC_PREFIX", clean_env);
|
|
|
|
RemoveEnv("COMPILER_PATH", clean_env);
|
|
|
|
RemoveEnv("COLLECT_GCC", clean_env);
|
2003-09-18 16:22:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
// Run GCC to assemble and link the program into native code.
|
|
|
|
//
|
|
|
|
// Note:
|
|
|
|
// We can't just assemble and link the file with the system assembler
|
|
|
|
// and linker because we don't know where to put the _start symbol.
|
|
|
|
// GCC mysteriously knows how to do it.
|
2004-12-19 18:00:56 +00:00
|
|
|
std::vector<const char*> args;
|
2005-04-10 20:59:38 +00:00
|
|
|
args.push_back(gcc.c_str());
|
2004-12-14 04:20:08 +00:00
|
|
|
args.push_back("-fno-strict-aliasing");
|
|
|
|
args.push_back("-O3");
|
|
|
|
args.push_back("-o");
|
2004-12-19 18:00:56 +00:00
|
|
|
args.push_back(OutputFilename.c_str());
|
|
|
|
args.push_back(InputFilename.c_str());
|
2005-04-20 04:08:35 +00:00
|
|
|
|
2005-09-23 06:05:46 +00:00
|
|
|
// StringsToDelete - We don't want to call c_str() on temporary strings.
|
|
|
|
// If we need a temporary string, copy it here so that the memory is not
|
|
|
|
// reclaimed until after the exec call. All of these strings are allocated
|
|
|
|
// with strdup.
|
|
|
|
std::vector<char*> StringsToDelete;
|
|
|
|
|
2005-02-28 08:45:35 +00:00
|
|
|
if (Shared) args.push_back("-shared");
|
2005-08-02 22:07:38 +00:00
|
|
|
if (ExportAllAsDynamic) args.push_back("-export-dynamic");
|
2005-02-28 08:45:35 +00:00
|
|
|
if (!RPath.empty()) {
|
|
|
|
std::string rp = "-Wl,-rpath," + RPath;
|
2005-09-23 06:05:46 +00:00
|
|
|
StringsToDelete.push_back(strdup(rp.c_str()));
|
|
|
|
args.push_back(StringsToDelete.back());
|
2005-02-28 08:45:35 +00:00
|
|
|
}
|
|
|
|
if (!SOName.empty()) {
|
|
|
|
std::string so = "-Wl,-soname," + SOName;
|
2005-09-23 06:05:46 +00:00
|
|
|
StringsToDelete.push_back(strdup(so.c_str()));
|
|
|
|
args.push_back(StringsToDelete.back());
|
2005-02-28 08:45:35 +00:00
|
|
|
}
|
2005-04-20 04:08:35 +00:00
|
|
|
|
2005-02-28 08:45:35 +00:00
|
|
|
// Add in the libpaths to find the libraries.
|
|
|
|
//
|
|
|
|
// Note:
|
|
|
|
// When gccld is called from the llvm-gxx frontends, the -L paths for
|
2005-04-22 00:00:37 +00:00
|
|
|
// the LLVM cfrontend install paths are appended. We don't want the
|
2005-02-28 08:45:35 +00:00
|
|
|
// native linker to use these -L paths as they contain bytecode files.
|
|
|
|
// Further, we don't want any -L paths that contain bytecode shared
|
|
|
|
// libraries or true bytecode archive files. We omit them in all such
|
|
|
|
// cases.
|
2005-09-23 06:05:46 +00:00
|
|
|
for (unsigned index = 0; index < LibPaths.size(); index++)
|
|
|
|
if (!isBytecodeLPath(LibPaths[index])) {
|
|
|
|
std::string Tmp = "-L"+LibPaths[index];
|
|
|
|
StringsToDelete.push_back(strdup(Tmp.c_str()));
|
|
|
|
args.push_back(StringsToDelete.back());
|
2005-02-28 08:45:35 +00:00
|
|
|
}
|
2005-04-22 00:00:37 +00:00
|
|
|
|
2003-09-19 20:24:23 +00:00
|
|
|
// Add in the libraries to link.
|
2005-09-23 06:05:46 +00:00
|
|
|
for (unsigned index = 0; index < Libraries.size(); index++)
|
2004-12-19 18:00:56 +00:00
|
|
|
if (Libraries[index] != "crtend") {
|
2005-09-23 06:05:46 +00:00
|
|
|
std::string Tmp = "-l"+Libraries[index];
|
|
|
|
StringsToDelete.push_back(strdup(Tmp.c_str()));
|
|
|
|
args.push_back(StringsToDelete.back());
|
2004-12-19 18:00:56 +00:00
|
|
|
}
|
2005-09-23 06:05:46 +00:00
|
|
|
args.push_back(0); // Null terminate.
|
2003-09-18 16:22:26 +00:00
|
|
|
|
2003-09-19 20:24:23 +00:00
|
|
|
// Run the compiler to assembly and link together the program.
|
2005-04-20 03:22:18 +00:00
|
|
|
if (Verbose) dumpArgs(&args[0]);
|
2005-09-23 06:05:46 +00:00
|
|
|
int Res = sys::Program::ExecuteAndWait(gcc, &args[0], (const char**)clean_env);
|
|
|
|
|
|
|
|
while (!StringsToDelete.empty()) {
|
|
|
|
free(StringsToDelete.back());
|
|
|
|
StringsToDelete.pop_back();
|
|
|
|
}
|
|
|
|
return Res;
|
2003-09-18 16:22:26 +00:00
|
|
|
}
|
2004-06-02 00:22:24 +00:00
|
|
|
|