blob: a9e91f9dc35d08b4d2b05941cfdfd83f65dae528 [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
Chris Lattner0ebee742004-06-02 00:22:24 +000096/// PrintAndReturn - Prints a message to standard error and returns true.
Misha Brukman98399692003-11-20 06:21:54 +000097///
98/// Inputs:
99/// progname - The name of the program (i.e. argv[0]).
100/// Message - The message to print to standard error.
Misha Brukman98399692003-11-20 06:21:54 +0000101///
Chris Lattner0ebee742004-06-02 00:22:24 +0000102static int PrintAndReturn(const char *progname, const std::string &Message) {
103 std::cerr << progname << ": " << Message << "\n";
John Criswell71478b72003-09-19 20:24:23 +0000104 return 1;
Chris Lattner10970eb2003-04-19 22:44:38 +0000105}
106
Chris Lattner7e88d412004-06-02 00:10:19 +0000107/// EmitShellScript - Output the wrapper file that invokes the JIT on the LLVM
108/// bytecode file for the program.
109static void EmitShellScript(char **argv) {
110 // Output the script to start the program...
111 std::ofstream Out2(OutputFilename.c_str());
112 if (!Out2.good())
113 exit(PrintAndReturn(argv[0], "error opening '" + OutputFilename +
114 "' for writing!"));
115
116 Out2 << "#!/bin/sh\n";
117 // Allow user to setenv LLVMINTERP if lli is not in their PATH.
118 Out2 << "lli=${LLVMINTERP-lli}\n";
119 Out2 << "exec $lli \\\n";
120 // gcc accepts -l<lib> and implicitly searches /lib and /usr/lib.
121 LibPaths.push_back("/lib");
122 LibPaths.push_back("/usr/lib");
123 LibPaths.push_back("/usr/X11R6/lib");
124 // We don't need to link in libc! In fact, /usr/lib/libc.so may not be a
125 // shared object at all! See RH 8: plain text.
126 std::vector<std::string>::iterator libc =
127 std::find(Libraries.begin(), Libraries.end(), "c");
128 if (libc != Libraries.end()) Libraries.erase(libc);
129 // List all the shared object (native) libraries this executable will need
130 // on the command line, so that we don't have to do this manually!
131 for (std::vector<std::string>::iterator i = Libraries.begin(),
132 e = Libraries.end(); i != e; ++i) {
133 std::string FullLibraryPath = FindLib(*i, LibPaths, true);
134 if (!FullLibraryPath.empty() && IsSharedObject(FullLibraryPath))
135 Out2 << " -load=" << FullLibraryPath << " \\\n";
136 }
137 Out2 << " $0.bc ${1+\"$@\"}\n";
138 Out2.close();
139}
Chris Lattner10970eb2003-04-19 22:44:38 +0000140
Misha Brukmane97fbed2003-09-30 17:59:25 +0000141int main(int argc, char **argv, char **envp) {
Chris Lattner5ff62e92002-07-22 02:10:13 +0000142 cl::ParseCommandLineOptions(argc, argv, " llvm linker for GCC\n");
Chris Lattner364d1202004-02-19 20:32:39 +0000143 PrintStackTraceOnErrorSignal();
Chris Lattnere7fca512002-01-24 19:12:12 +0000144
Misha Brukman98399692003-11-20 06:21:54 +0000145 std::string ModuleID("gccld-output");
Brian Gaeke00cc86a2003-11-05 22:13:00 +0000146 std::auto_ptr<Module> Composite(new Module(ModuleID));
Chris Lattnere7fca512002-01-24 19:12:12 +0000147
Brian Gaeke69a79602003-05-23 20:27:07 +0000148 // We always look first in the current directory when searching for libraries.
149 LibPaths.insert(LibPaths.begin(), ".");
150
Misha Brukmancf00c4a2003-10-10 17:57:28 +0000151 // If the user specified an extra search path in their environment, respect
152 // it.
Chris Lattnerd34a51d2003-04-21 19:53:24 +0000153 if (char *SearchPath = getenv("LLVM_LIB_SEARCH_PATH"))
154 LibPaths.push_back(SearchPath);
155
Chris Lattnerc65b1042003-04-19 23:07:33 +0000156 // Remove any consecutive duplicates of the same library...
157 Libraries.erase(std::unique(Libraries.begin(), Libraries.end()),
158 Libraries.end());
159
John Criswell71478b72003-09-19 20:24:23 +0000160 // Link in all of the files
Brian Gaekee98ddfc2003-09-30 14:03:48 +0000161 if (LinkFiles(argv[0], Composite.get(), InputFilenames, Verbose))
162 return 1; // Error already printed
Chris Lattnerfd35c642003-11-03 17:27:17 +0000163
164 if (!LinkAsLibrary)
165 LinkLibraries(argv[0], Composite.get(), Libraries, LibPaths,
166 Verbose, Native);
John Criswell71478b72003-09-19 20:24:23 +0000167
Chris Lattner10970eb2003-04-19 22:44:38 +0000168 // Link in all of the libraries next...
Chris Lattnere7fca512002-01-24 19:12:12 +0000169
John Criswelldc0de4f2003-09-18 16:22:26 +0000170 // Create the output file.
Chris Lattner10970eb2003-04-19 22:44:38 +0000171 std::string RealBytecodeOutput = OutputFilename;
172 if (!LinkAsLibrary) RealBytecodeOutput += ".bc";
173 std::ofstream Out(RealBytecodeOutput.c_str());
174 if (!Out.good())
175 return PrintAndReturn(argv[0], "error opening '" + RealBytecodeOutput +
176 "' for writing!");
Chris Lattnere7fca512002-01-24 19:12:12 +0000177
John Criswelldc0de4f2003-09-18 16:22:26 +0000178 // Ensure that the bytecode file gets removed from the disk if we get a
179 // SIGINT signal.
Chris Lattner10970eb2003-04-19 22:44:38 +0000180 RemoveFileOnSignal(RealBytecodeOutput);
Chris Lattner76d12292002-04-18 19:55:25 +0000181
John Criswelldc0de4f2003-09-18 16:22:26 +0000182 // Generate the bytecode file.
Misha Brukmane97fbed2003-09-30 17:59:25 +0000183 if (GenerateBytecode(Composite.get(), Strip, !NoInternalize, &Out)) {
John Criswelldc0de4f2003-09-18 16:22:26 +0000184 Out.close();
Brian Gaeke00cc86a2003-11-05 22:13:00 +0000185 return PrintAndReturn(argv[0], "error generating bytecode");
John Criswelldc0de4f2003-09-18 16:22:26 +0000186 }
187
John Criswelldc0de4f2003-09-18 16:22:26 +0000188 // Close the bytecode file.
Chris Lattnere7fca512002-01-24 19:12:12 +0000189 Out.close();
190
John Criswelldc0de4f2003-09-18 16:22:26 +0000191 // If we are not linking a library, generate either a native executable
192 // or a JIT shell script, depending upon what the user wants.
Chris Lattner10970eb2003-04-19 22:44:38 +0000193 if (!LinkAsLibrary) {
John Criswelldabaf7d2003-09-16 21:27:35 +0000194 // If the user wants to generate a native executable, compile it from the
195 // bytecode file.
196 //
197 // Otherwise, create a script that will run the bytecode through the JIT.
Chris Lattner6e271232003-09-22 20:21:34 +0000198 if (Native) {
John Criswelldc0de4f2003-09-18 16:22:26 +0000199 // Name of the Assembly Language output file
200 std::string AssemblyFile = OutputFilename + ".s";
201
John Criswelldc0de4f2003-09-18 16:22:26 +0000202 // Mark the output files for removal if we get an interrupt.
Misha Brukmane97fbed2003-09-30 17:59:25 +0000203 RemoveFileOnSignal(AssemblyFile);
204 RemoveFileOnSignal(OutputFilename);
John Criswelldabaf7d2003-09-16 21:27:35 +0000205
John Criswell83ca6ec2003-09-17 19:04:22 +0000206 // Determine the locations of the llc and gcc programs.
Misha Brukmane97fbed2003-09-30 17:59:25 +0000207 std::string llc = FindExecutable("llc", argv[0]);
208 std::string gcc = FindExecutable("gcc", argv[0]);
John Criswell83ca6ec2003-09-17 19:04:22 +0000209 if (llc.empty())
Misha Brukmane97fbed2003-09-30 17:59:25 +0000210 return PrintAndReturn(argv[0], "Failed to find llc");
John Criswell83ca6ec2003-09-17 19:04:22 +0000211
212 if (gcc.empty())
Misha Brukmane97fbed2003-09-30 17:59:25 +0000213 return PrintAndReturn(argv[0], "Failed to find gcc");
John Criswell83ca6ec2003-09-17 19:04:22 +0000214
John Criswelldc0de4f2003-09-18 16:22:26 +0000215 // Generate an assembly language file for the bytecode.
John Criswell71478b72003-09-19 20:24:23 +0000216 if (Verbose) std::cout << "Generating Assembly Code\n";
Chris Lattner6e271232003-09-22 20:21:34 +0000217 GenerateAssembly(AssemblyFile, RealBytecodeOutput, llc, envp);
John Criswell71478b72003-09-19 20:24:23 +0000218 if (Verbose) std::cout << "Generating Native Code\n";
Chris Lattner6e271232003-09-22 20:21:34 +0000219 GenerateNative(OutputFilename, AssemblyFile, Libraries, LibPaths,
220 gcc, envp);
John Criswelldabaf7d2003-09-16 21:27:35 +0000221
John Criswelldc0de4f2003-09-18 16:22:26 +0000222 // Remove the assembly language file.
John Criswelldc0de4f2003-09-18 16:22:26 +0000223 removeFile (AssemblyFile);
Chris Lattner69e8d282004-04-06 16:43:13 +0000224 } else if (NativeCBE) {
225 std::string CFile = OutputFilename + ".cbe.c";
226
227 // Mark the output files for removal if we get an interrupt.
228 RemoveFileOnSignal(CFile);
229 RemoveFileOnSignal(OutputFilename);
230
231 // Determine the locations of the llc and gcc programs.
232 std::string llc = FindExecutable("llc", argv[0]);
233 std::string gcc = FindExecutable("gcc", argv[0]);
234 if (llc.empty())
235 return PrintAndReturn(argv[0], "Failed to find llc");
236 if (gcc.empty())
237 return PrintAndReturn(argv[0], "Failed to find gcc");
238
239 // Generate an assembly language file for the bytecode.
240 if (Verbose) std::cout << "Generating Assembly Code\n";
241 GenerateCFile(CFile, RealBytecodeOutput, llc, envp);
242 if (Verbose) std::cout << "Generating Native Code\n";
243 GenerateNative(OutputFilename, CFile, Libraries, LibPaths, gcc, envp);
244
245 // Remove the assembly language file.
246 removeFile(CFile);
247
Chris Lattner6e271232003-09-22 20:21:34 +0000248 } else {
Chris Lattner7e88d412004-06-02 00:10:19 +0000249 EmitShellScript(argv);
John Criswelldabaf7d2003-09-16 21:27:35 +0000250 }
Chris Lattnere7fca512002-01-24 19:12:12 +0000251
Chris Lattner10970eb2003-04-19 22:44:38 +0000252 // Make the script executable...
Misha Brukmane97fbed2003-09-30 17:59:25 +0000253 MakeFileExecutable(OutputFilename);
Misha Brukmanc1fdca82003-08-20 20:38:15 +0000254
John Criswell22edc392003-09-02 21:11:22 +0000255 // Make the bytecode file readable and directly executable in LLEE as well
Misha Brukmane97fbed2003-09-30 17:59:25 +0000256 MakeFileExecutable(RealBytecodeOutput);
257 MakeFileReadable(RealBytecodeOutput);
Chris Lattner10970eb2003-04-19 22:44:38 +0000258 }
Chris Lattnere7fca512002-01-24 19:12:12 +0000259
260 return 0;
261}