1 //===- gccld.cpp - LLVM 'ld' compatible linker ----------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This utility is intended to be compatible with GCC, and follows standard
11 // system 'ld' conventions. As such, the default output file is ./a.out.
12 // Additionally, this program outputs a shell script that is used to invoke LLI
13 // to execute the program. In this manner, the generated executable (a.out for
14 // example), is directly executable, whereas the bytecode file actually lives in
15 // the a.out.bc file generated by this program. Also, Force is on by default.
17 // Note that if someone (or a script) deletes the executable program generated,
18 // the .bc file will be left around. Considering that this is a temporary hack,
19 // I'm not too worried about this.
21 //===----------------------------------------------------------------------===//
24 #include "llvm/Module.h"
25 #include "llvm/PassManager.h"
26 #include "llvm/Bytecode/Reader.h"
27 #include "llvm/Bytecode/WriteBytecodePass.h"
28 #include "llvm/Target/TargetData.h"
29 #include "llvm/Transforms/IPO.h"
30 #include "llvm/Transforms/Scalar.h"
31 #include "llvm/Transforms/Utils/Linker.h"
32 #include "Support/CommandLine.h"
33 #include "Support/FileUtilities.h"
34 #include "Support/Signals.h"
35 #include "Support/SystemUtils.h"
43 InputFilenames(cl::Positional, cl::desc("<input bytecode files>"),
47 OutputFilename("o", cl::desc("Override output filename"), cl::init("a.out"),
48 cl::value_desc("filename"));
51 Verbose("v", cl::desc("Print information about actions taken"));
54 LibPaths("L", cl::desc("Specify a library search path"), cl::Prefix,
55 cl::value_desc("directory"));
58 Libraries("l", cl::desc("Specify libraries to link to"), cl::Prefix,
59 cl::value_desc("library prefix"));
62 Strip("s", cl::desc("Strip symbol info from executable"));
65 NoInternalize("disable-internalize",
66 cl::desc("Do not mark all symbols as internal"));
68 ExportDynamic("export-dynamic", cl::desc("Alias for -disable-internalize"),
69 cl::aliasopt(NoInternalize));
72 LinkAsLibrary("link-as-library", cl::desc("Link the .bc files together as a"
73 " library, not an executable"));
75 Relink("r", cl::desc("Alias for -link-as-library"),
76 cl::aliasopt(LinkAsLibrary));
80 cl::desc("Generate a native binary instead of a shell script"));
82 // Compatibility options that are ignored but supported by LD
84 CO3("soname", cl::Hidden, cl::desc("Compatibility option: ignored"));
86 CO4("version-script", cl::Hidden, cl::desc("Compatibility option: ignored"));
88 CO5("eh-frame-hdr", cl::Hidden, cl::desc("Compatibility option: ignored"));
90 CO6("h", cl::Hidden, cl::desc("Compatibility option: ignored"));
95 /// PrintAndReturn - Prints a message to standard error and returns a value
96 /// usable for an exit status.
99 /// progname - The name of the program (i.e. argv[0]).
100 /// Message - The message to print to standard error.
101 /// Extra - Extra information to print between the program name and thei
102 /// message. It is optional.
105 /// Returns a value that can be used as the exit status (i.e. for exit()).
108 PrintAndReturn(const char *progname,
109 const std::string &Message,
110 const std::string &Extra)
112 std::cerr << progname << Extra << ": " << Message << "\n";
116 /// CopyEnv - This function takes an array of environment variables and makes a
117 /// copy of it. This copy can then be manipulated any way the caller likes
118 /// without affecting the process's real environment.
121 /// envp - An array of C strings containing an environment.
124 /// NULL - An error occurred.
126 /// Otherwise, a pointer to a new array of C strings is returned. Every string
127 /// in the array is a duplicate of the one in the original array (i.e. we do
128 /// not copy the char *'s from one array to another).
130 char ** CopyEnv(char ** const envp) {
131 // Count the number of entries in the old list;
132 unsigned entries; // The number of entries in the old environment list
133 for (entries = 0; envp[entries] != NULL; entries++)
136 // Add one more entry for the NULL pointer that ends the list.
139 // If there are no entries at all, just return NULL.
143 // Allocate a new environment list.
144 char **newenv = new char* [entries];
145 if ((newenv = new char* [entries]) == NULL)
148 // Make a copy of the list. Don't forget the NULL that ends the list.
150 while (envp[entries] != NULL) {
151 newenv[entries] = new char[strlen (envp[entries]) + 1];
152 strcpy (newenv[entries], envp[entries]);
155 newenv[entries] = NULL;
161 /// RemoveEnv - Remove the specified environment variable from the environment
165 /// name - The name of the variable to remove. It cannot be NULL.
166 /// envp - The array of environment variables. It cannot be NULL.
169 /// This is mainly done because functions to remove items from the environment
170 /// are not available across all platforms. In particular, Solaris does not
171 /// seem to have an unsetenv() function or a setenv() function (or they are
172 /// undocumented if they do exist).
174 void RemoveEnv(const char * name, char ** const envp) {
175 for (unsigned index=0; envp[index] != NULL; index++) {
176 // Find the first equals sign in the array and make it an EOS character.
177 char *p = strchr (envp[index], '=');
183 // Compare the two strings. If they are equal, zap this string.
184 // Otherwise, restore it.
185 if (!strcmp(name, envp[index]))
194 } // End llvm namespace
196 int main(int argc, char **argv, char **envp) {
197 cl::ParseCommandLineOptions(argc, argv, " llvm linker for GCC\n");
199 std::string ModuleID("gccld-output");
200 std::auto_ptr<Module> Composite(new Module(ModuleID));
202 // We always look first in the current directory when searching for libraries.
203 LibPaths.insert(LibPaths.begin(), ".");
205 // If the user specified an extra search path in their environment, respect
207 if (char *SearchPath = getenv("LLVM_LIB_SEARCH_PATH"))
208 LibPaths.push_back(SearchPath);
210 // Remove any consecutive duplicates of the same library...
211 Libraries.erase(std::unique(Libraries.begin(), Libraries.end()),
214 // Link in all of the files
215 if (LinkFiles(argv[0], Composite.get(), InputFilenames, Verbose))
216 return 1; // Error already printed
219 LinkLibraries(argv[0], Composite.get(), Libraries, LibPaths,
222 // Link in all of the libraries next...
224 // Create the output file.
225 std::string RealBytecodeOutput = OutputFilename;
226 if (!LinkAsLibrary) RealBytecodeOutput += ".bc";
227 std::ofstream Out(RealBytecodeOutput.c_str());
229 return PrintAndReturn(argv[0], "error opening '" + RealBytecodeOutput +
232 // Ensure that the bytecode file gets removed from the disk if we get a
234 RemoveFileOnSignal(RealBytecodeOutput);
236 // Generate the bytecode file.
237 if (GenerateBytecode(Composite.get(), Strip, !NoInternalize, &Out)) {
239 return PrintAndReturn(argv[0], "error generating bytecode");
242 // Close the bytecode file.
245 // If we are not linking a library, generate either a native executable
246 // or a JIT shell script, depending upon what the user wants.
247 if (!LinkAsLibrary) {
248 // If the user wants to generate a native executable, compile it from the
251 // Otherwise, create a script that will run the bytecode through the JIT.
253 // Name of the Assembly Language output file
254 std::string AssemblyFile = OutputFilename + ".s";
256 // Mark the output files for removal if we get an interrupt.
257 RemoveFileOnSignal(AssemblyFile);
258 RemoveFileOnSignal(OutputFilename);
260 // Determine the locations of the llc and gcc programs.
261 std::string llc = FindExecutable("llc", argv[0]);
262 std::string gcc = FindExecutable("gcc", argv[0]);
264 return PrintAndReturn(argv[0], "Failed to find llc");
267 return PrintAndReturn(argv[0], "Failed to find gcc");
269 // Generate an assembly language file for the bytecode.
270 if (Verbose) std::cout << "Generating Assembly Code\n";
271 GenerateAssembly(AssemblyFile, RealBytecodeOutput, llc, envp);
272 if (Verbose) std::cout << "Generating Native Code\n";
273 GenerateNative(OutputFilename, AssemblyFile, Libraries, LibPaths,
276 // Remove the assembly language file.
277 removeFile (AssemblyFile);
279 // Output the script to start the program...
280 std::ofstream Out2(OutputFilename.c_str());
282 return PrintAndReturn(argv[0], "error opening '" + OutputFilename +
284 Out2 << "#!/bin/sh\nlli \\\n";
285 // gcc accepts -l<lib> and implicitly searches /lib and /usr/lib.
286 LibPaths.push_back("/lib");
287 LibPaths.push_back("/usr/lib");
288 LibPaths.push_back("/usr/X11R6/lib");
289 // We don't need to link in libc! In fact, /usr/lib/libc.so may not be a
290 // shared object at all! See RH 8: plain text.
291 std::vector<std::string>::iterator libc =
292 std::find(Libraries.begin(), Libraries.end(), "c");
293 if (libc != Libraries.end()) Libraries.erase(libc);
294 // List all the shared object (native) libraries this executable will need
295 // on the command line, so that we don't have to do this manually!
296 for (std::vector<std::string>::iterator i = Libraries.begin(),
297 e = Libraries.end(); i != e; ++i) {
298 std::string FullLibraryPath = FindLib(*i, LibPaths, true);
299 if (!FullLibraryPath.empty() && IsSharedObject(FullLibraryPath))
300 Out2 << " -load=" << FullLibraryPath << " \\\n";
302 Out2 << " $0.bc $*\n";
306 // Make the script executable...
307 MakeFileExecutable(OutputFilename);
309 // Make the bytecode file readable and directly executable in LLEE as well
310 MakeFileExecutable(RealBytecodeOutput);
311 MakeFileReadable(RealBytecodeOutput);