blob: 74b9ff00b2be2b62a3c3cb50c9bd841f10bd8113 [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 Lattner76d12292002-04-18 19:55:25 +000034#include "Support/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"));
John Criswelldabaf7d2003-09-16 21:27:35 +000081
John Criswelldc0de4f2003-09-18 16:22:26 +000082 // Compatibility options that are ignored but supported by LD
Chris Lattnera856db22003-04-18 23:38:22 +000083 cl::opt<std::string>
84 CO3("soname", cl::Hidden, cl::desc("Compatibility option: ignored"));
85 cl::opt<std::string>
86 CO4("version-script", cl::Hidden, cl::desc("Compatibility option: ignored"));
87 cl::opt<bool>
88 CO5("eh-frame-hdr", cl::Hidden, cl::desc("Compatibility option: ignored"));
John Criswellaa2a47d2003-12-09 15:39:11 +000089 cl::opt<std::string>
90 CO6("h", cl::Hidden, cl::desc("Compatibility option: ignored"));
Chris Lattnerf3d4f172003-04-18 23:01:25 +000091}
Chris Lattnere7fca512002-01-24 19:12:12 +000092
Brian Gaeked0fde302003-11-11 22:41:34 +000093namespace llvm {
94
Misha Brukman98399692003-11-20 06:21:54 +000095/// PrintAndReturn - Prints a message to standard error and returns a value
96/// usable for an exit status.
97///
98/// Inputs:
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.
103///
104/// Return value:
105/// Returns a value that can be used as the exit status (i.e. for exit()).
106///
John Criswell71478b72003-09-19 20:24:23 +0000107int
Misha Brukman98399692003-11-20 06:21:54 +0000108PrintAndReturn(const char *progname,
109 const std::string &Message,
110 const std::string &Extra)
John Criswell71478b72003-09-19 20:24:23 +0000111{
112 std::cerr << progname << Extra << ": " << Message << "\n";
113 return 1;
Chris Lattner10970eb2003-04-19 22:44:38 +0000114}
115
Misha Brukman98399692003-11-20 06:21:54 +0000116/// 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.
119///
120/// Inputs:
121/// envp - An array of C strings containing an environment.
122///
123/// Return value:
124/// NULL - An error occurred.
125///
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).
129///
Misha Brukmane97fbed2003-09-30 17:59:25 +0000130char ** CopyEnv(char ** const envp) {
John Criswell71478b72003-09-19 20:24:23 +0000131 // Count the number of entries in the old list;
Chris Lattner6e271232003-09-22 20:21:34 +0000132 unsigned entries; // The number of entries in the old environment list
John Criswell71478b72003-09-19 20:24:23 +0000133 for (entries = 0; envp[entries] != NULL; entries++)
Chris Lattner9e6f6862003-11-29 19:45:47 +0000134 /*empty*/;
Chris Lattner7cb77e12003-05-13 22:14:13 +0000135
John Criswell71478b72003-09-19 20:24:23 +0000136 // Add one more entry for the NULL pointer that ends the list.
John Criswell71478b72003-09-19 20:24:23 +0000137 ++entries;
Chris Lattner10970eb2003-04-19 22:44:38 +0000138
John Criswell71478b72003-09-19 20:24:23 +0000139 // If there are no entries at all, just return NULL.
John Criswell71478b72003-09-19 20:24:23 +0000140 if (entries == 0)
John Criswell71478b72003-09-19 20:24:23 +0000141 return NULL;
Chris Lattner10970eb2003-04-19 22:44:38 +0000142
John Criswell71478b72003-09-19 20:24:23 +0000143 // Allocate a new environment list.
Chris Lattner9e6f6862003-11-29 19:45:47 +0000144 char **newenv = new char* [entries];
145 if ((newenv = new char* [entries]) == NULL)
John Criswell71478b72003-09-19 20:24:23 +0000146 return NULL;
Chris Lattner10970eb2003-04-19 22:44:38 +0000147
John Criswell71478b72003-09-19 20:24:23 +0000148 // Make a copy of the list. Don't forget the NULL that ends the list.
John Criswell71478b72003-09-19 20:24:23 +0000149 entries = 0;
Misha Brukmane97fbed2003-09-30 17:59:25 +0000150 while (envp[entries] != NULL) {
John Criswell71478b72003-09-19 20:24:23 +0000151 newenv[entries] = new char[strlen (envp[entries]) + 1];
152 strcpy (newenv[entries], envp[entries]);
153 ++entries;
154 }
155 newenv[entries] = NULL;
Chris Lattner10970eb2003-04-19 22:44:38 +0000156
John Criswell71478b72003-09-19 20:24:23 +0000157 return newenv;
158}
159
160
Misha Brukman98399692003-11-20 06:21:54 +0000161/// RemoveEnv - Remove the specified environment variable from the environment
162/// array.
163///
164/// Inputs:
165/// name - The name of the variable to remove. It cannot be NULL.
166/// envp - The array of environment variables. It cannot be NULL.
167///
168/// Notes:
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).
173///
Misha Brukmane97fbed2003-09-30 17:59:25 +0000174void RemoveEnv(const char * name, char ** const envp) {
Chris Lattner6e271232003-09-22 20:21:34 +0000175 for (unsigned index=0; envp[index] != NULL; index++) {
John Criswell71478b72003-09-19 20:24:23 +0000176 // Find the first equals sign in the array and make it an EOS character.
Chris Lattner6e271232003-09-22 20:21:34 +0000177 char *p = strchr (envp[index], '=');
John Criswell71478b72003-09-19 20:24:23 +0000178 if (p == NULL)
John Criswell71478b72003-09-19 20:24:23 +0000179 continue;
John Criswell71478b72003-09-19 20:24:23 +0000180 else
John Criswell71478b72003-09-19 20:24:23 +0000181 *p = '\0';
John Criswell71478b72003-09-19 20:24:23 +0000182
John Criswell71478b72003-09-19 20:24:23 +0000183 // Compare the two strings. If they are equal, zap this string.
184 // Otherwise, restore it.
Misha Brukmane97fbed2003-09-30 17:59:25 +0000185 if (!strcmp(name, envp[index]))
John Criswell71478b72003-09-19 20:24:23 +0000186 *envp[index] = '\0';
John Criswell71478b72003-09-19 20:24:23 +0000187 else
John Criswell71478b72003-09-19 20:24:23 +0000188 *p = '=';
Chris Lattner10970eb2003-04-19 22:44:38 +0000189 }
John Criswell71478b72003-09-19 20:24:23 +0000190
191 return;
Chris Lattner10970eb2003-04-19 22:44:38 +0000192}
193
Brian Gaeked0fde302003-11-11 22:41:34 +0000194} // End llvm namespace
Chris Lattner10970eb2003-04-19 22:44:38 +0000195
Misha Brukmane97fbed2003-09-30 17:59:25 +0000196int main(int argc, char **argv, char **envp) {
Chris Lattner5ff62e92002-07-22 02:10:13 +0000197 cl::ParseCommandLineOptions(argc, argv, " llvm linker for GCC\n");
Chris Lattnere7fca512002-01-24 19:12:12 +0000198
Misha Brukman98399692003-11-20 06:21:54 +0000199 std::string ModuleID("gccld-output");
Brian Gaeke00cc86a2003-11-05 22:13:00 +0000200 std::auto_ptr<Module> Composite(new Module(ModuleID));
Chris Lattnere7fca512002-01-24 19:12:12 +0000201
Brian Gaeke69a79602003-05-23 20:27:07 +0000202 // We always look first in the current directory when searching for libraries.
203 LibPaths.insert(LibPaths.begin(), ".");
204
Misha Brukmancf00c4a2003-10-10 17:57:28 +0000205 // If the user specified an extra search path in their environment, respect
206 // it.
Chris Lattnerd34a51d2003-04-21 19:53:24 +0000207 if (char *SearchPath = getenv("LLVM_LIB_SEARCH_PATH"))
208 LibPaths.push_back(SearchPath);
209
Chris Lattnerc65b1042003-04-19 23:07:33 +0000210 // Remove any consecutive duplicates of the same library...
211 Libraries.erase(std::unique(Libraries.begin(), Libraries.end()),
212 Libraries.end());
213
John Criswell71478b72003-09-19 20:24:23 +0000214 // Link in all of the files
Brian Gaekee98ddfc2003-09-30 14:03:48 +0000215 if (LinkFiles(argv[0], Composite.get(), InputFilenames, Verbose))
216 return 1; // Error already printed
Chris Lattnerfd35c642003-11-03 17:27:17 +0000217
218 if (!LinkAsLibrary)
219 LinkLibraries(argv[0], Composite.get(), Libraries, LibPaths,
220 Verbose, Native);
John Criswell71478b72003-09-19 20:24:23 +0000221
Chris Lattner10970eb2003-04-19 22:44:38 +0000222 // Link in all of the libraries next...
Chris Lattnere7fca512002-01-24 19:12:12 +0000223
John Criswelldc0de4f2003-09-18 16:22:26 +0000224 // Create the output file.
Chris Lattner10970eb2003-04-19 22:44:38 +0000225 std::string RealBytecodeOutput = OutputFilename;
226 if (!LinkAsLibrary) RealBytecodeOutput += ".bc";
227 std::ofstream Out(RealBytecodeOutput.c_str());
228 if (!Out.good())
229 return PrintAndReturn(argv[0], "error opening '" + RealBytecodeOutput +
230 "' for writing!");
Chris Lattnere7fca512002-01-24 19:12:12 +0000231
John Criswelldc0de4f2003-09-18 16:22:26 +0000232 // Ensure that the bytecode file gets removed from the disk if we get a
233 // SIGINT signal.
Chris Lattner10970eb2003-04-19 22:44:38 +0000234 RemoveFileOnSignal(RealBytecodeOutput);
Chris Lattner76d12292002-04-18 19:55:25 +0000235
John Criswelldc0de4f2003-09-18 16:22:26 +0000236 // Generate the bytecode file.
Misha Brukmane97fbed2003-09-30 17:59:25 +0000237 if (GenerateBytecode(Composite.get(), Strip, !NoInternalize, &Out)) {
John Criswelldc0de4f2003-09-18 16:22:26 +0000238 Out.close();
Brian Gaeke00cc86a2003-11-05 22:13:00 +0000239 return PrintAndReturn(argv[0], "error generating bytecode");
John Criswelldc0de4f2003-09-18 16:22:26 +0000240 }
241
John Criswelldc0de4f2003-09-18 16:22:26 +0000242 // Close the bytecode file.
Chris Lattnere7fca512002-01-24 19:12:12 +0000243 Out.close();
244
John Criswelldc0de4f2003-09-18 16:22:26 +0000245 // If we are not linking a library, generate either a native executable
246 // or a JIT shell script, depending upon what the user wants.
Chris Lattner10970eb2003-04-19 22:44:38 +0000247 if (!LinkAsLibrary) {
John Criswelldabaf7d2003-09-16 21:27:35 +0000248 // If the user wants to generate a native executable, compile it from the
249 // bytecode file.
250 //
251 // Otherwise, create a script that will run the bytecode through the JIT.
Chris Lattner6e271232003-09-22 20:21:34 +0000252 if (Native) {
John Criswelldc0de4f2003-09-18 16:22:26 +0000253 // Name of the Assembly Language output file
254 std::string AssemblyFile = OutputFilename + ".s";
255
John Criswelldc0de4f2003-09-18 16:22:26 +0000256 // Mark the output files for removal if we get an interrupt.
Misha Brukmane97fbed2003-09-30 17:59:25 +0000257 RemoveFileOnSignal(AssemblyFile);
258 RemoveFileOnSignal(OutputFilename);
John Criswelldabaf7d2003-09-16 21:27:35 +0000259
John Criswell83ca6ec2003-09-17 19:04:22 +0000260 // Determine the locations of the llc and gcc programs.
Misha Brukmane97fbed2003-09-30 17:59:25 +0000261 std::string llc = FindExecutable("llc", argv[0]);
262 std::string gcc = FindExecutable("gcc", argv[0]);
John Criswell83ca6ec2003-09-17 19:04:22 +0000263 if (llc.empty())
Misha Brukmane97fbed2003-09-30 17:59:25 +0000264 return PrintAndReturn(argv[0], "Failed to find llc");
John Criswell83ca6ec2003-09-17 19:04:22 +0000265
266 if (gcc.empty())
Misha Brukmane97fbed2003-09-30 17:59:25 +0000267 return PrintAndReturn(argv[0], "Failed to find gcc");
John Criswell83ca6ec2003-09-17 19:04:22 +0000268
John Criswelldc0de4f2003-09-18 16:22:26 +0000269 // Generate an assembly language file for the bytecode.
John Criswell71478b72003-09-19 20:24:23 +0000270 if (Verbose) std::cout << "Generating Assembly Code\n";
Chris Lattner6e271232003-09-22 20:21:34 +0000271 GenerateAssembly(AssemblyFile, RealBytecodeOutput, llc, envp);
John Criswell71478b72003-09-19 20:24:23 +0000272 if (Verbose) std::cout << "Generating Native Code\n";
Chris Lattner6e271232003-09-22 20:21:34 +0000273 GenerateNative(OutputFilename, AssemblyFile, Libraries, LibPaths,
274 gcc, envp);
John Criswelldabaf7d2003-09-16 21:27:35 +0000275
John Criswelldc0de4f2003-09-18 16:22:26 +0000276 // Remove the assembly language file.
John Criswelldc0de4f2003-09-18 16:22:26 +0000277 removeFile (AssemblyFile);
Chris Lattner6e271232003-09-22 20:21:34 +0000278 } else {
John Criswelldabaf7d2003-09-16 21:27:35 +0000279 // Output the script to start the program...
280 std::ofstream Out2(OutputFilename.c_str());
281 if (!Out2.good())
282 return PrintAndReturn(argv[0], "error opening '" + OutputFilename +
283 "' for writing!");
Misha Brukman3e25f2e2003-11-20 19:08:42 +0000284 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");
Misha Brukman0d2459a2003-11-24 05:31:57 +0000288 LibPaths.push_back("/usr/X11R6/lib");
Misha Brukman3e25f2e2003-11-20 19:08:42 +0000289 // 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);
Misha Brukman3e15c7a2003-11-24 05:29:42 +0000299 if (!FullLibraryPath.empty() && IsSharedObject(FullLibraryPath))
Misha Brukman3e25f2e2003-11-20 19:08:42 +0000300 Out2 << " -load=" << FullLibraryPath << " \\\n";
301 }
302 Out2 << " $0.bc $*\n";
John Criswelldabaf7d2003-09-16 21:27:35 +0000303 Out2.close();
304 }
Chris Lattnere7fca512002-01-24 19:12:12 +0000305
Chris Lattner10970eb2003-04-19 22:44:38 +0000306 // Make the script executable...
Misha Brukmane97fbed2003-09-30 17:59:25 +0000307 MakeFileExecutable(OutputFilename);
Misha Brukmanc1fdca82003-08-20 20:38:15 +0000308
John Criswell22edc392003-09-02 21:11:22 +0000309 // Make the bytecode file readable and directly executable in LLEE as well
Misha Brukmane97fbed2003-09-30 17:59:25 +0000310 MakeFileExecutable(RealBytecodeOutput);
311 MakeFileReadable(RealBytecodeOutput);
Chris Lattner10970eb2003-04-19 22:44:38 +0000312 }
Chris Lattnere7fca512002-01-24 19:12:12 +0000313
314 return 0;
315}