blob: 1adf08e4c9e587544445a4143bd759ec35af2950 [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
Chris Lattnerf3d4f172003-04-18 23:01:25 +000039namespace {
40 cl::list<std::string>
41 InputFilenames(cl::Positional, cl::desc("<input bytecode files>"),
42 cl::OneOrMore);
Chris Lattner5ff62e92002-07-22 02:10:13 +000043
Chris Lattnerf3d4f172003-04-18 23:01:25 +000044 cl::opt<std::string>
45 OutputFilename("o", cl::desc("Override output filename"), cl::init("a.out"),
46 cl::value_desc("filename"));
Chris Lattner5ff62e92002-07-22 02:10:13 +000047
Chris Lattnerf3d4f172003-04-18 23:01:25 +000048 cl::opt<bool>
49 Verbose("v", cl::desc("Print information about actions taken"));
50
51 cl::list<std::string>
52 LibPaths("L", cl::desc("Specify a library search path"), cl::Prefix,
53 cl::value_desc("directory"));
Chris Lattner5ff62e92002-07-22 02:10:13 +000054
Chris Lattnerf3d4f172003-04-18 23:01:25 +000055 cl::list<std::string>
56 Libraries("l", cl::desc("Specify libraries to link to"), cl::Prefix,
57 cl::value_desc("library prefix"));
Chris Lattner5ff62e92002-07-22 02:10:13 +000058
Chris Lattnerf3d4f172003-04-18 23:01:25 +000059 cl::opt<bool>
60 Strip("s", cl::desc("Strip symbol info from executable"));
Chris Lattner5ff62e92002-07-22 02:10:13 +000061
Chris Lattnerf3d4f172003-04-18 23:01:25 +000062 cl::opt<bool>
63 NoInternalize("disable-internalize",
64 cl::desc("Do not mark all symbols as internal"));
Chris Lattnera2b2dc92003-08-22 19:18:45 +000065 static cl::alias
66 ExportDynamic("export-dynamic", cl::desc("Alias for -disable-internalize"),
67 cl::aliasopt(NoInternalize));
Chris Lattnera856db22003-04-18 23:38:22 +000068
Chris Lattner10970eb2003-04-19 22:44:38 +000069 cl::opt<bool>
70 LinkAsLibrary("link-as-library", cl::desc("Link the .bc files together as a"
71 " library, not an executable"));
72
John Criswelldabaf7d2003-09-16 21:27:35 +000073 cl::opt<bool>
Chris Lattner6e271232003-09-22 20:21:34 +000074 Native("native",
75 cl::desc("Generate a native binary instead of a shell script"));
John Criswelldabaf7d2003-09-16 21:27:35 +000076
John Criswelldc0de4f2003-09-18 16:22:26 +000077 // Compatibility options that are ignored but supported by LD
Chris Lattnera856db22003-04-18 23:38:22 +000078 cl::opt<std::string>
79 CO3("soname", cl::Hidden, cl::desc("Compatibility option: ignored"));
80 cl::opt<std::string>
81 CO4("version-script", cl::Hidden, cl::desc("Compatibility option: ignored"));
82 cl::opt<bool>
83 CO5("eh-frame-hdr", cl::Hidden, cl::desc("Compatibility option: ignored"));
Chris Lattner6ac79d12003-05-27 19:15:11 +000084 cl::opt<bool>
85 CO6("r", cl::Hidden, cl::desc("Compatibility option: ignored"));
Chris Lattnerf3d4f172003-04-18 23:01:25 +000086}
Chris Lattnere7fca512002-01-24 19:12:12 +000087
Chris Lattner10970eb2003-04-19 22:44:38 +000088//
John Criswell71478b72003-09-19 20:24:23 +000089// Function: PrintAndReturn ()
90//
91// Description:
92// Prints a message (usually error message) to standard error (stderr) and
93// returns a value usable for an exit status.
94//
95// Inputs:
96// progname - The name of the program (i.e. argv[0]).
97// Message - The message to print to standard error.
98// Extra - Extra information to print between the program name and thei
99// message. It is optional.
100//
101// Outputs:
102// None.
103//
104// Return value:
105// Returns a value that can be used as the exit status (i.e. for exit()).
106//
107int
108PrintAndReturn (const char *progname,
109 const std::string &Message,
110 const std::string &Extra)
111{
112 std::cerr << progname << Extra << ": " << Message << "\n";
113 return 1;
Chris Lattner10970eb2003-04-19 22:44:38 +0000114}
115
John Criswell71478b72003-09-19 20:24:23 +0000116//
117//
118// Function: CopyEnv()
119//
120// Description:
121// This function takes an array of environment variables and makes a
122// copy of it. This copy can then be manipulated any way the caller likes
123// without affecting the process's real environment.
124//
125// Inputs:
126// envp - An array of C strings containing an environment.
127//
128// Outputs:
129// None.
130//
131// Return value:
132// NULL - An error occurred.
133//
134// Otherwise, a pointer to a new array of C strings is returned. Every string
135// in the array is a duplicate of the one in the original array (i.e. we do
136// not copy the char *'s from one array to another).
137//
Misha Brukmane97fbed2003-09-30 17:59:25 +0000138char ** CopyEnv(char ** const envp) {
John Criswell71478b72003-09-19 20:24:23 +0000139 // Count the number of entries in the old list;
Chris Lattner6e271232003-09-22 20:21:34 +0000140 unsigned entries; // The number of entries in the old environment list
John Criswell71478b72003-09-19 20:24:23 +0000141 for (entries = 0; envp[entries] != NULL; entries++)
142 {
143 ;
Chris Lattner7cb77e12003-05-13 22:14:13 +0000144 }
145
John Criswell71478b72003-09-19 20:24:23 +0000146 // Add one more entry for the NULL pointer that ends the list.
John Criswell71478b72003-09-19 20:24:23 +0000147 ++entries;
Chris Lattner10970eb2003-04-19 22:44:38 +0000148
John Criswell71478b72003-09-19 20:24:23 +0000149 // If there are no entries at all, just return NULL.
John Criswell71478b72003-09-19 20:24:23 +0000150 if (entries == 0)
John Criswell71478b72003-09-19 20:24:23 +0000151 return NULL;
Chris Lattner10970eb2003-04-19 22:44:38 +0000152
John Criswell71478b72003-09-19 20:24:23 +0000153 // Allocate a new environment list.
Chris Lattner6e271232003-09-22 20:21:34 +0000154 char **newenv;
John Criswell71478b72003-09-19 20:24:23 +0000155 if ((newenv = new (char *) [entries]) == NULL)
John Criswell71478b72003-09-19 20:24:23 +0000156 return NULL;
Chris Lattner10970eb2003-04-19 22:44:38 +0000157
John Criswell71478b72003-09-19 20:24:23 +0000158 // Make a copy of the list. Don't forget the NULL that ends the list.
John Criswell71478b72003-09-19 20:24:23 +0000159 entries = 0;
Misha Brukmane97fbed2003-09-30 17:59:25 +0000160 while (envp[entries] != NULL) {
John Criswell71478b72003-09-19 20:24:23 +0000161 newenv[entries] = new char[strlen (envp[entries]) + 1];
162 strcpy (newenv[entries], envp[entries]);
163 ++entries;
164 }
165 newenv[entries] = NULL;
Chris Lattner10970eb2003-04-19 22:44:38 +0000166
John Criswell71478b72003-09-19 20:24:23 +0000167 return newenv;
168}
169
170
171//
172// Function: RemoveEnv()
173//
174// Description:
175// Remove the specified environment variable from the environment array.
176//
177// Inputs:
178// name - The name of the variable to remove. It cannot be NULL.
179// envp - The array of environment variables. It cannot be NULL.
180//
181// Outputs:
182// envp - The pointer to the specified variable name is removed.
183//
184// Return value:
185// None.
186//
187// Notes:
188// This is mainly done because functions to remove items from the environment
189// are not available across all platforms. In particular, Solaris does not
190// seem to have an unsetenv() function or a setenv() function (or they are
191// undocumented if they do exist).
192//
Misha Brukmane97fbed2003-09-30 17:59:25 +0000193void RemoveEnv(const char * name, char ** const envp) {
Chris Lattner6e271232003-09-22 20:21:34 +0000194 for (unsigned index=0; envp[index] != NULL; index++) {
John Criswell71478b72003-09-19 20:24:23 +0000195 // Find the first equals sign in the array and make it an EOS character.
Chris Lattner6e271232003-09-22 20:21:34 +0000196 char *p = strchr (envp[index], '=');
John Criswell71478b72003-09-19 20:24:23 +0000197 if (p == NULL)
John Criswell71478b72003-09-19 20:24:23 +0000198 continue;
John Criswell71478b72003-09-19 20:24:23 +0000199 else
John Criswell71478b72003-09-19 20:24:23 +0000200 *p = '\0';
John Criswell71478b72003-09-19 20:24:23 +0000201
John Criswell71478b72003-09-19 20:24:23 +0000202 // Compare the two strings. If they are equal, zap this string.
203 // Otherwise, restore it.
Misha Brukmane97fbed2003-09-30 17:59:25 +0000204 if (!strcmp(name, envp[index]))
John Criswell71478b72003-09-19 20:24:23 +0000205 *envp[index] = '\0';
John Criswell71478b72003-09-19 20:24:23 +0000206 else
John Criswell71478b72003-09-19 20:24:23 +0000207 *p = '=';
Chris Lattner10970eb2003-04-19 22:44:38 +0000208 }
John Criswell71478b72003-09-19 20:24:23 +0000209
210 return;
Chris Lattner10970eb2003-04-19 22:44:38 +0000211}
212
Chris Lattner10970eb2003-04-19 22:44:38 +0000213
Misha Brukmane97fbed2003-09-30 17:59:25 +0000214int main(int argc, char **argv, char **envp) {
Chris Lattner5ff62e92002-07-22 02:10:13 +0000215 cl::ParseCommandLineOptions(argc, argv, " llvm linker for GCC\n");
Chris Lattnere7fca512002-01-24 19:12:12 +0000216
Chris Lattnere7fca512002-01-24 19:12:12 +0000217 std::string ErrorMessage;
Chris Lattner10970eb2003-04-19 22:44:38 +0000218 std::auto_ptr<Module> Composite(LoadObject(InputFilenames[0], ErrorMessage));
219 if (Composite.get() == 0)
220 return PrintAndReturn(argv[0], ErrorMessage);
Chris Lattnere7fca512002-01-24 19:12:12 +0000221
Brian Gaeke69a79602003-05-23 20:27:07 +0000222 // We always look first in the current directory when searching for libraries.
223 LibPaths.insert(LibPaths.begin(), ".");
224
Misha Brukmancf00c4a2003-10-10 17:57:28 +0000225 // If the user specified an extra search path in their environment, respect
226 // it.
Chris Lattnerd34a51d2003-04-21 19:53:24 +0000227 if (char *SearchPath = getenv("LLVM_LIB_SEARCH_PATH"))
228 LibPaths.push_back(SearchPath);
229
Chris Lattnerc65b1042003-04-19 23:07:33 +0000230 // Remove any consecutive duplicates of the same library...
231 Libraries.erase(std::unique(Libraries.begin(), Libraries.end()),
232 Libraries.end());
233
John Criswell71478b72003-09-19 20:24:23 +0000234 // Link in all of the files
Brian Gaekee98ddfc2003-09-30 14:03:48 +0000235 if (LinkFiles(argv[0], Composite.get(), InputFilenames, Verbose))
236 return 1; // Error already printed
Chris Lattnerfd35c642003-11-03 17:27:17 +0000237
238 if (!LinkAsLibrary)
239 LinkLibraries(argv[0], Composite.get(), Libraries, LibPaths,
240 Verbose, Native);
John Criswell71478b72003-09-19 20:24:23 +0000241
Chris Lattner10970eb2003-04-19 22:44:38 +0000242 // Link in all of the libraries next...
Chris Lattnere7fca512002-01-24 19:12:12 +0000243
John Criswelldc0de4f2003-09-18 16:22:26 +0000244 // Create the output file.
Chris Lattner10970eb2003-04-19 22:44:38 +0000245 std::string RealBytecodeOutput = OutputFilename;
246 if (!LinkAsLibrary) RealBytecodeOutput += ".bc";
247 std::ofstream Out(RealBytecodeOutput.c_str());
248 if (!Out.good())
249 return PrintAndReturn(argv[0], "error opening '" + RealBytecodeOutput +
250 "' for writing!");
Chris Lattnere7fca512002-01-24 19:12:12 +0000251
John Criswelldc0de4f2003-09-18 16:22:26 +0000252 // Ensure that the bytecode file gets removed from the disk if we get a
253 // SIGINT signal.
Chris Lattner10970eb2003-04-19 22:44:38 +0000254 RemoveFileOnSignal(RealBytecodeOutput);
Chris Lattner76d12292002-04-18 19:55:25 +0000255
John Criswelldc0de4f2003-09-18 16:22:26 +0000256 // Generate the bytecode file.
Misha Brukmane97fbed2003-09-30 17:59:25 +0000257 if (GenerateBytecode(Composite.get(), Strip, !NoInternalize, &Out)) {
John Criswelldc0de4f2003-09-18 16:22:26 +0000258 Out.close();
259 return PrintAndReturn(argv[0], "error generating bytcode");
260 }
261
John Criswelldc0de4f2003-09-18 16:22:26 +0000262 // Close the bytecode file.
Chris Lattnere7fca512002-01-24 19:12:12 +0000263 Out.close();
264
John Criswelldc0de4f2003-09-18 16:22:26 +0000265 // If we are not linking a library, generate either a native executable
266 // or a JIT shell script, depending upon what the user wants.
Chris Lattner10970eb2003-04-19 22:44:38 +0000267 if (!LinkAsLibrary) {
John Criswelldabaf7d2003-09-16 21:27:35 +0000268 // If the user wants to generate a native executable, compile it from the
269 // bytecode file.
270 //
271 // Otherwise, create a script that will run the bytecode through the JIT.
Chris Lattner6e271232003-09-22 20:21:34 +0000272 if (Native) {
John Criswelldc0de4f2003-09-18 16:22:26 +0000273 // Name of the Assembly Language output file
274 std::string AssemblyFile = OutputFilename + ".s";
275
John Criswelldc0de4f2003-09-18 16:22:26 +0000276 // Mark the output files for removal if we get an interrupt.
Misha Brukmane97fbed2003-09-30 17:59:25 +0000277 RemoveFileOnSignal(AssemblyFile);
278 RemoveFileOnSignal(OutputFilename);
John Criswelldabaf7d2003-09-16 21:27:35 +0000279
John Criswell83ca6ec2003-09-17 19:04:22 +0000280 // Determine the locations of the llc and gcc programs.
Misha Brukmane97fbed2003-09-30 17:59:25 +0000281 std::string llc = FindExecutable("llc", argv[0]);
282 std::string gcc = FindExecutable("gcc", argv[0]);
John Criswell83ca6ec2003-09-17 19:04:22 +0000283 if (llc.empty())
Misha Brukmane97fbed2003-09-30 17:59:25 +0000284 return PrintAndReturn(argv[0], "Failed to find llc");
John Criswell83ca6ec2003-09-17 19:04:22 +0000285
286 if (gcc.empty())
Misha Brukmane97fbed2003-09-30 17:59:25 +0000287 return PrintAndReturn(argv[0], "Failed to find gcc");
John Criswell83ca6ec2003-09-17 19:04:22 +0000288
John Criswelldc0de4f2003-09-18 16:22:26 +0000289 // Generate an assembly language file for the bytecode.
John Criswell71478b72003-09-19 20:24:23 +0000290 if (Verbose) std::cout << "Generating Assembly Code\n";
Chris Lattner6e271232003-09-22 20:21:34 +0000291 GenerateAssembly(AssemblyFile, RealBytecodeOutput, llc, envp);
John Criswell71478b72003-09-19 20:24:23 +0000292 if (Verbose) std::cout << "Generating Native Code\n";
Chris Lattner6e271232003-09-22 20:21:34 +0000293 GenerateNative(OutputFilename, AssemblyFile, Libraries, LibPaths,
294 gcc, envp);
John Criswelldabaf7d2003-09-16 21:27:35 +0000295
John Criswelldc0de4f2003-09-18 16:22:26 +0000296 // Remove the assembly language file.
John Criswelldc0de4f2003-09-18 16:22:26 +0000297 removeFile (AssemblyFile);
Chris Lattner6e271232003-09-22 20:21:34 +0000298 } else {
John Criswelldabaf7d2003-09-16 21:27:35 +0000299 // Output the script to start the program...
300 std::ofstream Out2(OutputFilename.c_str());
301 if (!Out2.good())
302 return PrintAndReturn(argv[0], "error opening '" + OutputFilename +
303 "' for writing!");
Brian Gaeke7c7a3a22003-10-24 20:00:06 +0000304 Out2 << "#!/bin/sh\nlli $0.bc $*\n";
John Criswelldabaf7d2003-09-16 21:27:35 +0000305 Out2.close();
306 }
Chris Lattnere7fca512002-01-24 19:12:12 +0000307
Chris Lattner10970eb2003-04-19 22:44:38 +0000308 // Make the script executable...
Misha Brukmane97fbed2003-09-30 17:59:25 +0000309 MakeFileExecutable(OutputFilename);
Misha Brukmanc1fdca82003-08-20 20:38:15 +0000310
John Criswell22edc392003-09-02 21:11:22 +0000311 // Make the bytecode file readable and directly executable in LLEE as well
Misha Brukmane97fbed2003-09-30 17:59:25 +0000312 MakeFileExecutable(RealBytecodeOutput);
313 MakeFileReadable(RealBytecodeOutput);
Chris Lattner10970eb2003-04-19 22:44:38 +0000314 }
Chris Lattnere7fca512002-01-24 19:12:12 +0000315
316 return 0;
317}