blob: e4614dc03ed31b5655711740f0f20ee44b90701e [file] [log] [blame]
Chris Lattner10970eb2003-04-19 22:44:38 +00001//===- gccld.cpp - LLVM 'ld' compatible linker ----------------------------===//
John Criswell7c0e0222003-10-20 17:47:21 +00002//
3// The LLVM Compiler Infrastructure
4//
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.
7//
8//===----------------------------------------------------------------------===//
Chris Lattnere7fca512002-01-24 19:12:12 +00009//
10// This utility is intended to be compatible with GCC, and follows standard
Chris Lattner10970eb2003-04-19 22:44:38 +000011// system 'ld' conventions. As such, the default output file is ./a.out.
Chris Lattnere7fca512002-01-24 19:12:12 +000012// 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.
16//
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,
Brian Gaeke69a79602003-05-23 20:27:07 +000019// I'm not too worried about this.
Chris Lattnere7fca512002-01-24 19:12:12 +000020//
21//===----------------------------------------------------------------------===//
22
Chris Lattner6e271232003-09-22 20:21:34 +000023#include "gccld.h"
Chris Lattnere7fca512002-01-24 19:12:12 +000024#include "llvm/Module.h"
Chris Lattnerad202a02002-04-08 00:14:58 +000025#include "llvm/PassManager.h"
26#include "llvm/Bytecode/Reader.h"
27#include "llvm/Bytecode/WriteBytecodePass.h"
Chris Lattner9c3b55e2003-04-24 19:13:02 +000028#include "llvm/Target/TargetData.h"
Chris Lattnerd9d8c072002-07-23 22:04:43 +000029#include "llvm/Transforms/IPO.h"
Chris Lattnerd9d8c072002-07-23 22:04:43 +000030#include "llvm/Transforms/Scalar.h"
Misha Brukmane97fbed2003-09-30 17:59:25 +000031#include "llvm/Transforms/Utils/Linker.h"
Chris Lattnere7fca512002-01-24 19:12:12 +000032#include "Support/CommandLine.h"
Misha Brukmane97fbed2003-09-30 17:59:25 +000033#include "Support/FileUtilities.h"
Chris Lattnerbed85ff2004-05-27 05:41:36 +000034#include "llvm/System/Signals.h"
Misha Brukmane97fbed2003-09-30 17:59:25 +000035#include "Support/SystemUtils.h"
Chris Lattnere7fca512002-01-24 19:12:12 +000036#include <fstream>
37#include <memory>
Chris Lattnere7fca512002-01-24 19:12:12 +000038
Brian Gaeked0fde302003-11-11 22:41:34 +000039using namespace llvm;
40
Chris Lattnerf3d4f172003-04-18 23:01:25 +000041namespace {
42 cl::list<std::string>
43 InputFilenames(cl::Positional, cl::desc("<input bytecode files>"),
44 cl::OneOrMore);
Chris Lattner5ff62e92002-07-22 02:10:13 +000045
Chris Lattnerf3d4f172003-04-18 23:01:25 +000046 cl::opt<std::string>
47 OutputFilename("o", cl::desc("Override output filename"), cl::init("a.out"),
48 cl::value_desc("filename"));
Chris Lattner5ff62e92002-07-22 02:10:13 +000049
Chris Lattnerf3d4f172003-04-18 23:01:25 +000050 cl::opt<bool>
51 Verbose("v", cl::desc("Print information about actions taken"));
52
53 cl::list<std::string>
54 LibPaths("L", cl::desc("Specify a library search path"), cl::Prefix,
55 cl::value_desc("directory"));
Chris Lattner5ff62e92002-07-22 02:10:13 +000056
Chris Lattnerf3d4f172003-04-18 23:01:25 +000057 cl::list<std::string>
58 Libraries("l", cl::desc("Specify libraries to link to"), cl::Prefix,
59 cl::value_desc("library prefix"));
Chris Lattner5ff62e92002-07-22 02:10:13 +000060
Chris Lattnerf3d4f172003-04-18 23:01:25 +000061 cl::opt<bool>
62 Strip("s", cl::desc("Strip symbol info from executable"));
Chris Lattner5ff62e92002-07-22 02:10:13 +000063
Chris Lattnerf3d4f172003-04-18 23:01:25 +000064 cl::opt<bool>
65 NoInternalize("disable-internalize",
66 cl::desc("Do not mark all symbols as internal"));
Chris Lattnerfe32bf52003-11-05 06:05:21 +000067 cl::alias
Chris Lattnera2b2dc92003-08-22 19:18:45 +000068 ExportDynamic("export-dynamic", cl::desc("Alias for -disable-internalize"),
69 cl::aliasopt(NoInternalize));
Chris Lattnera856db22003-04-18 23:38:22 +000070
Chris Lattner10970eb2003-04-19 22:44:38 +000071 cl::opt<bool>
72 LinkAsLibrary("link-as-library", cl::desc("Link the .bc files together as a"
73 " library, not an executable"));
Chris Lattnerfe32bf52003-11-05 06:05:21 +000074 cl::alias
75 Relink("r", cl::desc("Alias for -link-as-library"),
76 cl::aliasopt(LinkAsLibrary));
Chris Lattner10970eb2003-04-19 22:44:38 +000077
John Criswelldabaf7d2003-09-16 21:27:35 +000078 cl::opt<bool>
Chris Lattner6e271232003-09-22 20:21:34 +000079 Native("native",
80 cl::desc("Generate a native binary instead of a shell script"));
Chris Lattner69e8d282004-04-06 16:43:13 +000081 cl::opt<bool>
82 NativeCBE("native-cbe",
83 cl::desc("Generate a native binary with the C backend and GCC"));
John Criswelldabaf7d2003-09-16 21:27:35 +000084
John Criswelldc0de4f2003-09-18 16:22:26 +000085 // Compatibility options that are ignored but supported by LD
Chris Lattnera856db22003-04-18 23:38:22 +000086 cl::opt<std::string>
87 CO3("soname", cl::Hidden, cl::desc("Compatibility option: ignored"));
88 cl::opt<std::string>
89 CO4("version-script", cl::Hidden, cl::desc("Compatibility option: ignored"));
90 cl::opt<bool>
91 CO5("eh-frame-hdr", cl::Hidden, cl::desc("Compatibility option: ignored"));
John Criswellaa2a47d2003-12-09 15:39:11 +000092 cl::opt<std::string>
93 CO6("h", cl::Hidden, cl::desc("Compatibility option: ignored"));
Chris Lattnerf3d4f172003-04-18 23:01:25 +000094}
Chris Lattnere7fca512002-01-24 19:12:12 +000095
Brian Gaeked0fde302003-11-11 22:41:34 +000096namespace llvm {
97
Misha Brukman98399692003-11-20 06:21:54 +000098/// PrintAndReturn - Prints a message to standard error and returns a value
99/// usable for an exit status.
100///
101/// Inputs:
102/// progname - The name of the program (i.e. argv[0]).
103/// Message - The message to print to standard error.
104/// Extra - Extra information to print between the program name and thei
105/// message. It is optional.
106///
107/// Return value:
108/// Returns a value that can be used as the exit status (i.e. for exit()).
109///
John Criswell71478b72003-09-19 20:24:23 +0000110int
Misha Brukman98399692003-11-20 06:21:54 +0000111PrintAndReturn(const char *progname,
112 const std::string &Message,
113 const std::string &Extra)
John Criswell71478b72003-09-19 20:24:23 +0000114{
115 std::cerr << progname << Extra << ": " << Message << "\n";
116 return 1;
Chris Lattner10970eb2003-04-19 22:44:38 +0000117}
118
Misha Brukman98399692003-11-20 06:21:54 +0000119/// CopyEnv - This function takes an array of environment variables and makes a
120/// copy of it. This copy can then be manipulated any way the caller likes
121/// without affecting the process's real environment.
122///
123/// Inputs:
124/// envp - An array of C strings containing an environment.
125///
126/// Return value:
127/// NULL - An error occurred.
128///
129/// Otherwise, a pointer to a new array of C strings is returned. Every string
130/// in the array is a duplicate of the one in the original array (i.e. we do
131/// not copy the char *'s from one array to another).
132///
Misha Brukmane97fbed2003-09-30 17:59:25 +0000133char ** CopyEnv(char ** const envp) {
John Criswell71478b72003-09-19 20:24:23 +0000134 // Count the number of entries in the old list;
Chris Lattner6e271232003-09-22 20:21:34 +0000135 unsigned entries; // The number of entries in the old environment list
John Criswell71478b72003-09-19 20:24:23 +0000136 for (entries = 0; envp[entries] != NULL; entries++)
Chris Lattner9e6f6862003-11-29 19:45:47 +0000137 /*empty*/;
Chris Lattner7cb77e12003-05-13 22:14:13 +0000138
John Criswell71478b72003-09-19 20:24:23 +0000139 // Add one more entry for the NULL pointer that ends the list.
John Criswell71478b72003-09-19 20:24:23 +0000140 ++entries;
Chris Lattner10970eb2003-04-19 22:44:38 +0000141
John Criswell71478b72003-09-19 20:24:23 +0000142 // If there are no entries at all, just return NULL.
John Criswell71478b72003-09-19 20:24:23 +0000143 if (entries == 0)
John Criswell71478b72003-09-19 20:24:23 +0000144 return NULL;
Chris Lattner10970eb2003-04-19 22:44:38 +0000145
John Criswell71478b72003-09-19 20:24:23 +0000146 // Allocate a new environment list.
Chris Lattner9e6f6862003-11-29 19:45:47 +0000147 char **newenv = new char* [entries];
148 if ((newenv = new char* [entries]) == NULL)
John Criswell71478b72003-09-19 20:24:23 +0000149 return NULL;
Chris Lattner10970eb2003-04-19 22:44:38 +0000150
John Criswell71478b72003-09-19 20:24:23 +0000151 // Make a copy of the list. Don't forget the NULL that ends the list.
John Criswell71478b72003-09-19 20:24:23 +0000152 entries = 0;
Misha Brukmane97fbed2003-09-30 17:59:25 +0000153 while (envp[entries] != NULL) {
John Criswell71478b72003-09-19 20:24:23 +0000154 newenv[entries] = new char[strlen (envp[entries]) + 1];
155 strcpy (newenv[entries], envp[entries]);
156 ++entries;
157 }
158 newenv[entries] = NULL;
Chris Lattner10970eb2003-04-19 22:44:38 +0000159
John Criswell71478b72003-09-19 20:24:23 +0000160 return newenv;
161}
162
163
Misha Brukman98399692003-11-20 06:21:54 +0000164/// RemoveEnv - Remove the specified environment variable from the environment
165/// array.
166///
167/// Inputs:
168/// name - The name of the variable to remove. It cannot be NULL.
169/// envp - The array of environment variables. It cannot be NULL.
170///
171/// Notes:
172/// This is mainly done because functions to remove items from the environment
173/// are not available across all platforms. In particular, Solaris does not
174/// seem to have an unsetenv() function or a setenv() function (or they are
175/// undocumented if they do exist).
176///
Misha Brukmane97fbed2003-09-30 17:59:25 +0000177void RemoveEnv(const char * name, char ** const envp) {
Chris Lattner6e271232003-09-22 20:21:34 +0000178 for (unsigned index=0; envp[index] != NULL; index++) {
John Criswell71478b72003-09-19 20:24:23 +0000179 // Find the first equals sign in the array and make it an EOS character.
Chris Lattner6e271232003-09-22 20:21:34 +0000180 char *p = strchr (envp[index], '=');
John Criswell71478b72003-09-19 20:24:23 +0000181 if (p == NULL)
John Criswell71478b72003-09-19 20:24:23 +0000182 continue;
John Criswell71478b72003-09-19 20:24:23 +0000183 else
John Criswell71478b72003-09-19 20:24:23 +0000184 *p = '\0';
John Criswell71478b72003-09-19 20:24:23 +0000185
John Criswell71478b72003-09-19 20:24:23 +0000186 // Compare the two strings. If they are equal, zap this string.
187 // Otherwise, restore it.
Misha Brukmane97fbed2003-09-30 17:59:25 +0000188 if (!strcmp(name, envp[index]))
John Criswell71478b72003-09-19 20:24:23 +0000189 *envp[index] = '\0';
John Criswell71478b72003-09-19 20:24:23 +0000190 else
John Criswell71478b72003-09-19 20:24:23 +0000191 *p = '=';
Chris Lattner10970eb2003-04-19 22:44:38 +0000192 }
John Criswell71478b72003-09-19 20:24:23 +0000193
194 return;
Chris Lattner10970eb2003-04-19 22:44:38 +0000195}
196
Chris Lattner7e88d412004-06-02 00:10:19 +0000197} // end LLVM namespace
198
199/// EmitShellScript - Output the wrapper file that invokes the JIT on the LLVM
200/// bytecode file for the program.
201static void EmitShellScript(char **argv) {
202 // Output the script to start the program...
203 std::ofstream Out2(OutputFilename.c_str());
204 if (!Out2.good())
205 exit(PrintAndReturn(argv[0], "error opening '" + OutputFilename +
206 "' for writing!"));
207
208 Out2 << "#!/bin/sh\n";
209 // Allow user to setenv LLVMINTERP if lli is not in their PATH.
210 Out2 << "lli=${LLVMINTERP-lli}\n";
211 Out2 << "exec $lli \\\n";
212 // gcc accepts -l<lib> and implicitly searches /lib and /usr/lib.
213 LibPaths.push_back("/lib");
214 LibPaths.push_back("/usr/lib");
215 LibPaths.push_back("/usr/X11R6/lib");
216 // We don't need to link in libc! In fact, /usr/lib/libc.so may not be a
217 // shared object at all! See RH 8: plain text.
218 std::vector<std::string>::iterator libc =
219 std::find(Libraries.begin(), Libraries.end(), "c");
220 if (libc != Libraries.end()) Libraries.erase(libc);
221 // List all the shared object (native) libraries this executable will need
222 // on the command line, so that we don't have to do this manually!
223 for (std::vector<std::string>::iterator i = Libraries.begin(),
224 e = Libraries.end(); i != e; ++i) {
225 std::string FullLibraryPath = FindLib(*i, LibPaths, true);
226 if (!FullLibraryPath.empty() && IsSharedObject(FullLibraryPath))
227 Out2 << " -load=" << FullLibraryPath << " \\\n";
228 }
229 Out2 << " $0.bc ${1+\"$@\"}\n";
230 Out2.close();
231}
Chris Lattner10970eb2003-04-19 22:44:38 +0000232
Misha Brukmane97fbed2003-09-30 17:59:25 +0000233int main(int argc, char **argv, char **envp) {
Chris Lattner5ff62e92002-07-22 02:10:13 +0000234 cl::ParseCommandLineOptions(argc, argv, " llvm linker for GCC\n");
Chris Lattner364d1202004-02-19 20:32:39 +0000235 PrintStackTraceOnErrorSignal();
Chris Lattnere7fca512002-01-24 19:12:12 +0000236
Misha Brukman98399692003-11-20 06:21:54 +0000237 std::string ModuleID("gccld-output");
Brian Gaeke00cc86a2003-11-05 22:13:00 +0000238 std::auto_ptr<Module> Composite(new Module(ModuleID));
Chris Lattnere7fca512002-01-24 19:12:12 +0000239
Brian Gaeke69a79602003-05-23 20:27:07 +0000240 // We always look first in the current directory when searching for libraries.
241 LibPaths.insert(LibPaths.begin(), ".");
242
Misha Brukmancf00c4a2003-10-10 17:57:28 +0000243 // If the user specified an extra search path in their environment, respect
244 // it.
Chris Lattnerd34a51d2003-04-21 19:53:24 +0000245 if (char *SearchPath = getenv("LLVM_LIB_SEARCH_PATH"))
246 LibPaths.push_back(SearchPath);
247
Chris Lattnerc65b1042003-04-19 23:07:33 +0000248 // Remove any consecutive duplicates of the same library...
249 Libraries.erase(std::unique(Libraries.begin(), Libraries.end()),
250 Libraries.end());
251
John Criswell71478b72003-09-19 20:24:23 +0000252 // Link in all of the files
Brian Gaekee98ddfc2003-09-30 14:03:48 +0000253 if (LinkFiles(argv[0], Composite.get(), InputFilenames, Verbose))
254 return 1; // Error already printed
Chris Lattnerfd35c642003-11-03 17:27:17 +0000255
256 if (!LinkAsLibrary)
257 LinkLibraries(argv[0], Composite.get(), Libraries, LibPaths,
258 Verbose, Native);
John Criswell71478b72003-09-19 20:24:23 +0000259
Chris Lattner10970eb2003-04-19 22:44:38 +0000260 // Link in all of the libraries next...
Chris Lattnere7fca512002-01-24 19:12:12 +0000261
John Criswelldc0de4f2003-09-18 16:22:26 +0000262 // Create the output file.
Chris Lattner10970eb2003-04-19 22:44:38 +0000263 std::string RealBytecodeOutput = OutputFilename;
264 if (!LinkAsLibrary) RealBytecodeOutput += ".bc";
265 std::ofstream Out(RealBytecodeOutput.c_str());
266 if (!Out.good())
267 return PrintAndReturn(argv[0], "error opening '" + RealBytecodeOutput +
268 "' for writing!");
Chris Lattnere7fca512002-01-24 19:12:12 +0000269
John Criswelldc0de4f2003-09-18 16:22:26 +0000270 // Ensure that the bytecode file gets removed from the disk if we get a
271 // SIGINT signal.
Chris Lattner10970eb2003-04-19 22:44:38 +0000272 RemoveFileOnSignal(RealBytecodeOutput);
Chris Lattner76d12292002-04-18 19:55:25 +0000273
John Criswelldc0de4f2003-09-18 16:22:26 +0000274 // Generate the bytecode file.
Misha Brukmane97fbed2003-09-30 17:59:25 +0000275 if (GenerateBytecode(Composite.get(), Strip, !NoInternalize, &Out)) {
John Criswelldc0de4f2003-09-18 16:22:26 +0000276 Out.close();
Brian Gaeke00cc86a2003-11-05 22:13:00 +0000277 return PrintAndReturn(argv[0], "error generating bytecode");
John Criswelldc0de4f2003-09-18 16:22:26 +0000278 }
279
John Criswelldc0de4f2003-09-18 16:22:26 +0000280 // Close the bytecode file.
Chris Lattnere7fca512002-01-24 19:12:12 +0000281 Out.close();
282
John Criswelldc0de4f2003-09-18 16:22:26 +0000283 // If we are not linking a library, generate either a native executable
284 // or a JIT shell script, depending upon what the user wants.
Chris Lattner10970eb2003-04-19 22:44:38 +0000285 if (!LinkAsLibrary) {
John Criswelldabaf7d2003-09-16 21:27:35 +0000286 // If the user wants to generate a native executable, compile it from the
287 // bytecode file.
288 //
289 // Otherwise, create a script that will run the bytecode through the JIT.
Chris Lattner6e271232003-09-22 20:21:34 +0000290 if (Native) {
John Criswelldc0de4f2003-09-18 16:22:26 +0000291 // Name of the Assembly Language output file
292 std::string AssemblyFile = OutputFilename + ".s";
293
John Criswelldc0de4f2003-09-18 16:22:26 +0000294 // Mark the output files for removal if we get an interrupt.
Misha Brukmane97fbed2003-09-30 17:59:25 +0000295 RemoveFileOnSignal(AssemblyFile);
296 RemoveFileOnSignal(OutputFilename);
John Criswelldabaf7d2003-09-16 21:27:35 +0000297
John Criswell83ca6ec2003-09-17 19:04:22 +0000298 // Determine the locations of the llc and gcc programs.
Misha Brukmane97fbed2003-09-30 17:59:25 +0000299 std::string llc = FindExecutable("llc", argv[0]);
300 std::string gcc = FindExecutable("gcc", argv[0]);
John Criswell83ca6ec2003-09-17 19:04:22 +0000301 if (llc.empty())
Misha Brukmane97fbed2003-09-30 17:59:25 +0000302 return PrintAndReturn(argv[0], "Failed to find llc");
John Criswell83ca6ec2003-09-17 19:04:22 +0000303
304 if (gcc.empty())
Misha Brukmane97fbed2003-09-30 17:59:25 +0000305 return PrintAndReturn(argv[0], "Failed to find gcc");
John Criswell83ca6ec2003-09-17 19:04:22 +0000306
John Criswelldc0de4f2003-09-18 16:22:26 +0000307 // Generate an assembly language file for the bytecode.
John Criswell71478b72003-09-19 20:24:23 +0000308 if (Verbose) std::cout << "Generating Assembly Code\n";
Chris Lattner6e271232003-09-22 20:21:34 +0000309 GenerateAssembly(AssemblyFile, RealBytecodeOutput, llc, envp);
John Criswell71478b72003-09-19 20:24:23 +0000310 if (Verbose) std::cout << "Generating Native Code\n";
Chris Lattner6e271232003-09-22 20:21:34 +0000311 GenerateNative(OutputFilename, AssemblyFile, Libraries, LibPaths,
312 gcc, envp);
John Criswelldabaf7d2003-09-16 21:27:35 +0000313
John Criswelldc0de4f2003-09-18 16:22:26 +0000314 // Remove the assembly language file.
John Criswelldc0de4f2003-09-18 16:22:26 +0000315 removeFile (AssemblyFile);
Chris Lattner69e8d282004-04-06 16:43:13 +0000316 } else if (NativeCBE) {
317 std::string CFile = OutputFilename + ".cbe.c";
318
319 // Mark the output files for removal if we get an interrupt.
320 RemoveFileOnSignal(CFile);
321 RemoveFileOnSignal(OutputFilename);
322
323 // Determine the locations of the llc and gcc programs.
324 std::string llc = FindExecutable("llc", argv[0]);
325 std::string gcc = FindExecutable("gcc", argv[0]);
326 if (llc.empty())
327 return PrintAndReturn(argv[0], "Failed to find llc");
328 if (gcc.empty())
329 return PrintAndReturn(argv[0], "Failed to find gcc");
330
331 // Generate an assembly language file for the bytecode.
332 if (Verbose) std::cout << "Generating Assembly Code\n";
333 GenerateCFile(CFile, RealBytecodeOutput, llc, envp);
334 if (Verbose) std::cout << "Generating Native Code\n";
335 GenerateNative(OutputFilename, CFile, Libraries, LibPaths, gcc, envp);
336
337 // Remove the assembly language file.
338 removeFile(CFile);
339
Chris Lattner6e271232003-09-22 20:21:34 +0000340 } else {
Chris Lattner7e88d412004-06-02 00:10:19 +0000341 EmitShellScript(argv);
John Criswelldabaf7d2003-09-16 21:27:35 +0000342 }
Chris Lattnere7fca512002-01-24 19:12:12 +0000343
Chris Lattner10970eb2003-04-19 22:44:38 +0000344 // Make the script executable...
Misha Brukmane97fbed2003-09-30 17:59:25 +0000345 MakeFileExecutable(OutputFilename);
Misha Brukmanc1fdca82003-08-20 20:38:15 +0000346
John Criswell22edc392003-09-02 21:11:22 +0000347 // Make the bytecode file readable and directly executable in LLEE as well
Misha Brukmane97fbed2003-09-30 17:59:25 +0000348 MakeFileExecutable(RealBytecodeOutput);
349 MakeFileReadable(RealBytecodeOutput);
Chris Lattner10970eb2003-04-19 22:44:38 +0000350 }
Chris Lattnere7fca512002-01-24 19:12:12 +0000351
352 return 0;
353}