blob: b991cd57ea0b0364dbd2aac41c4ffa3714e89b22 [file] [log] [blame]
Chris Lattner10970eb2003-04-19 22:44:38 +00001//===- gccld.cpp - LLVM 'ld' compatible linker ----------------------------===//
Chris Lattnere7fca512002-01-24 19:12:12 +00002//
3// This utility is intended to be compatible with GCC, and follows standard
Chris Lattner10970eb2003-04-19 22:44:38 +00004// system 'ld' conventions. As such, the default output file is ./a.out.
Chris Lattnere7fca512002-01-24 19:12:12 +00005// Additionally, this program outputs a shell script that is used to invoke LLI
6// to execute the program. In this manner, the generated executable (a.out for
7// example), is directly executable, whereas the bytecode file actually lives in
8// the a.out.bc file generated by this program. Also, Force is on by default.
9//
10// Note that if someone (or a script) deletes the executable program generated,
11// the .bc file will be left around. Considering that this is a temporary hack,
Brian Gaeke69a79602003-05-23 20:27:07 +000012// I'm not too worried about this.
Chris Lattnere7fca512002-01-24 19:12:12 +000013//
14//===----------------------------------------------------------------------===//
15
Chris Lattner6e271232003-09-22 20:21:34 +000016#include "gccld.h"
Chris Lattnerc8cc4cb2002-05-07 18:36:35 +000017#include "llvm/Transforms/Utils/Linker.h"
Chris Lattnere7fca512002-01-24 19:12:12 +000018#include "llvm/Module.h"
Chris Lattnerad202a02002-04-08 00:14:58 +000019#include "llvm/PassManager.h"
20#include "llvm/Bytecode/Reader.h"
21#include "llvm/Bytecode/WriteBytecodePass.h"
Chris Lattner9c3b55e2003-04-24 19:13:02 +000022#include "llvm/Target/TargetData.h"
Chris Lattnerd9d8c072002-07-23 22:04:43 +000023#include "llvm/Transforms/IPO.h"
Chris Lattnerd9d8c072002-07-23 22:04:43 +000024#include "llvm/Transforms/Scalar.h"
John Criswelld35b5b52003-09-02 20:17:20 +000025#include "Support/FileUtilities.h"
John Criswell621727c2003-09-17 15:20:51 +000026#include "Support/SystemUtils.h"
Chris Lattnere7fca512002-01-24 19:12:12 +000027#include "Support/CommandLine.h"
Chris Lattner76d12292002-04-18 19:55:25 +000028#include "Support/Signals.h"
John Criswelldc0de4f2003-09-18 16:22:26 +000029
Chris Lattnere7fca512002-01-24 19:12:12 +000030#include <fstream>
31#include <memory>
Chris Lattnere7fca512002-01-24 19:12:12 +000032
Chris Lattnerf3d4f172003-04-18 23:01:25 +000033namespace {
34 cl::list<std::string>
35 InputFilenames(cl::Positional, cl::desc("<input bytecode files>"),
36 cl::OneOrMore);
Chris Lattner5ff62e92002-07-22 02:10:13 +000037
Chris Lattnerf3d4f172003-04-18 23:01:25 +000038 cl::opt<std::string>
39 OutputFilename("o", cl::desc("Override output filename"), cl::init("a.out"),
40 cl::value_desc("filename"));
Chris Lattner5ff62e92002-07-22 02:10:13 +000041
Chris Lattnerf3d4f172003-04-18 23:01:25 +000042 cl::opt<bool>
43 Verbose("v", cl::desc("Print information about actions taken"));
44
45 cl::list<std::string>
46 LibPaths("L", cl::desc("Specify a library search path"), cl::Prefix,
47 cl::value_desc("directory"));
Chris Lattner5ff62e92002-07-22 02:10:13 +000048
Chris Lattnerf3d4f172003-04-18 23:01:25 +000049 cl::list<std::string>
50 Libraries("l", cl::desc("Specify libraries to link to"), cl::Prefix,
51 cl::value_desc("library prefix"));
Chris Lattner5ff62e92002-07-22 02:10:13 +000052
Chris Lattnerf3d4f172003-04-18 23:01:25 +000053 cl::opt<bool>
54 Strip("s", cl::desc("Strip symbol info from executable"));
Chris Lattner5ff62e92002-07-22 02:10:13 +000055
Chris Lattnerf3d4f172003-04-18 23:01:25 +000056 cl::opt<bool>
57 NoInternalize("disable-internalize",
58 cl::desc("Do not mark all symbols as internal"));
Chris Lattnera2b2dc92003-08-22 19:18:45 +000059 static cl::alias
60 ExportDynamic("export-dynamic", cl::desc("Alias for -disable-internalize"),
61 cl::aliasopt(NoInternalize));
Chris Lattnera856db22003-04-18 23:38:22 +000062
Chris Lattner10970eb2003-04-19 22:44:38 +000063 cl::opt<bool>
64 LinkAsLibrary("link-as-library", cl::desc("Link the .bc files together as a"
65 " library, not an executable"));
66
John Criswelldabaf7d2003-09-16 21:27:35 +000067 cl::opt<bool>
Chris Lattner6e271232003-09-22 20:21:34 +000068 Native("native",
69 cl::desc("Generate a native binary instead of a shell script"));
John Criswelldabaf7d2003-09-16 21:27:35 +000070
John Criswelldc0de4f2003-09-18 16:22:26 +000071 // Compatibility options that are ignored but supported by LD
Chris Lattnera856db22003-04-18 23:38:22 +000072 cl::opt<std::string>
73 CO3("soname", cl::Hidden, cl::desc("Compatibility option: ignored"));
74 cl::opt<std::string>
75 CO4("version-script", cl::Hidden, cl::desc("Compatibility option: ignored"));
76 cl::opt<bool>
77 CO5("eh-frame-hdr", cl::Hidden, cl::desc("Compatibility option: ignored"));
Chris Lattner6ac79d12003-05-27 19:15:11 +000078 cl::opt<bool>
79 CO6("r", cl::Hidden, cl::desc("Compatibility option: ignored"));
Chris Lattnerf3d4f172003-04-18 23:01:25 +000080}
Chris Lattnere7fca512002-01-24 19:12:12 +000081
Chris Lattner10970eb2003-04-19 22:44:38 +000082//
John Criswell71478b72003-09-19 20:24:23 +000083// Function: PrintAndReturn ()
84//
85// Description:
86// Prints a message (usually error message) to standard error (stderr) and
87// returns a value usable for an exit status.
88//
89// Inputs:
90// progname - The name of the program (i.e. argv[0]).
91// Message - The message to print to standard error.
92// Extra - Extra information to print between the program name and thei
93// message. It is optional.
94//
95// Outputs:
96// None.
97//
98// Return value:
99// Returns a value that can be used as the exit status (i.e. for exit()).
100//
101int
102PrintAndReturn (const char *progname,
103 const std::string &Message,
104 const std::string &Extra)
105{
106 std::cerr << progname << Extra << ": " << Message << "\n";
107 return 1;
Chris Lattner10970eb2003-04-19 22:44:38 +0000108}
109
Chris Lattner10970eb2003-04-19 22:44:38 +0000110
John Criswell71478b72003-09-19 20:24:23 +0000111//
112//
113// Function: CopyEnv()
114//
115// Description:
116// 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// Outputs:
124// None.
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//
133char **
134CopyEnv (char ** const envp)
135{
John Criswell71478b72003-09-19 20:24:23 +0000136 //
137 // Count the number of entries in the old list;
138 //
Chris Lattner6e271232003-09-22 20:21:34 +0000139 unsigned entries; // The number of entries in the old environment list
John Criswell71478b72003-09-19 20:24:23 +0000140 for (entries = 0; envp[entries] != NULL; entries++)
141 {
142 ;
Chris Lattner7cb77e12003-05-13 22:14:13 +0000143 }
144
John Criswell71478b72003-09-19 20:24:23 +0000145 //
146 // Add one more entry for the NULL pointer that ends the list.
147 //
148 ++entries;
Chris Lattner10970eb2003-04-19 22:44:38 +0000149
John Criswell71478b72003-09-19 20:24:23 +0000150 //
151 // If there are no entries at all, just return NULL.
152 //
153 if (entries == 0)
154 {
155 return NULL;
156 }
Chris Lattner10970eb2003-04-19 22:44:38 +0000157
John Criswell71478b72003-09-19 20:24:23 +0000158 //
159 // Allocate a new environment list.
160 //
Chris Lattner6e271232003-09-22 20:21:34 +0000161 char **newenv;
John Criswell71478b72003-09-19 20:24:23 +0000162 if ((newenv = new (char *) [entries]) == NULL)
163 {
164 return NULL;
165 }
Chris Lattner10970eb2003-04-19 22:44:38 +0000166
John Criswell71478b72003-09-19 20:24:23 +0000167 //
168 // Make a copy of the list. Don't forget the NULL that ends the list.
169 //
170 entries = 0;
171 while (envp[entries] != NULL)
172 {
173 newenv[entries] = new char[strlen (envp[entries]) + 1];
174 strcpy (newenv[entries], envp[entries]);
175 ++entries;
176 }
177 newenv[entries] = NULL;
Chris Lattner10970eb2003-04-19 22:44:38 +0000178
John Criswell71478b72003-09-19 20:24:23 +0000179 return newenv;
180}
181
182
183//
184// Function: RemoveEnv()
185//
186// Description:
187// Remove the specified environment variable from the environment array.
188//
189// Inputs:
190// name - The name of the variable to remove. It cannot be NULL.
191// envp - The array of environment variables. It cannot be NULL.
192//
193// Outputs:
194// envp - The pointer to the specified variable name is removed.
195//
196// Return value:
197// None.
198//
199// Notes:
200// This is mainly done because functions to remove items from the environment
201// are not available across all platforms. In particular, Solaris does not
202// seem to have an unsetenv() function or a setenv() function (or they are
203// undocumented if they do exist).
204//
205void
206RemoveEnv (const char * name, char ** const envp)
207{
Chris Lattner6e271232003-09-22 20:21:34 +0000208 for (unsigned index=0; envp[index] != NULL; index++) {
John Criswell71478b72003-09-19 20:24:23 +0000209 // Find the first equals sign in the array and make it an EOS character.
Chris Lattner6e271232003-09-22 20:21:34 +0000210 char *p = strchr (envp[index], '=');
John Criswell71478b72003-09-19 20:24:23 +0000211 if (p == NULL)
212 {
213 continue;
214 }
215 else
216 {
217 *p = '\0';
218 }
219
John Criswell71478b72003-09-19 20:24:23 +0000220 // Compare the two strings. If they are equal, zap this string.
221 // Otherwise, restore it.
222 //
223 if (!strcmp (name, envp[index]))
224 {
225 *envp[index] = '\0';
226 }
227 else
228 {
229 *p = '=';
Chris Lattner10970eb2003-04-19 22:44:38 +0000230 }
231 }
John Criswell71478b72003-09-19 20:24:23 +0000232
233 return;
Chris Lattner10970eb2003-04-19 22:44:38 +0000234}
235
Chris Lattner10970eb2003-04-19 22:44:38 +0000236
John Criswelldc0de4f2003-09-18 16:22:26 +0000237int
238main(int argc, char **argv, char ** envp)
John Criswell621727c2003-09-17 15:20:51 +0000239{
Chris Lattner5ff62e92002-07-22 02:10:13 +0000240 cl::ParseCommandLineOptions(argc, argv, " llvm linker for GCC\n");
Chris Lattnere7fca512002-01-24 19:12:12 +0000241
Chris Lattnere7fca512002-01-24 19:12:12 +0000242 std::string ErrorMessage;
Chris Lattner10970eb2003-04-19 22:44:38 +0000243 std::auto_ptr<Module> Composite(LoadObject(InputFilenames[0], ErrorMessage));
244 if (Composite.get() == 0)
245 return PrintAndReturn(argv[0], ErrorMessage);
Chris Lattnere7fca512002-01-24 19:12:12 +0000246
Brian Gaeke69a79602003-05-23 20:27:07 +0000247 // We always look first in the current directory when searching for libraries.
248 LibPaths.insert(LibPaths.begin(), ".");
249
Chris Lattnerd34a51d2003-04-21 19:53:24 +0000250 // If the user specied an extra search path in their environment, respect it.
251 if (char *SearchPath = getenv("LLVM_LIB_SEARCH_PATH"))
252 LibPaths.push_back(SearchPath);
253
Chris Lattnerc65b1042003-04-19 23:07:33 +0000254 // Remove any consecutive duplicates of the same library...
255 Libraries.erase(std::unique(Libraries.begin(), Libraries.end()),
256 Libraries.end());
257
John Criswell71478b72003-09-19 20:24:23 +0000258 // Link in all of the files
Brian Gaekee98ddfc2003-09-30 14:03:48 +0000259 if (LinkFiles(argv[0], Composite.get(), InputFilenames, Verbose))
260 return 1; // Error already printed
Chris Lattner6e271232003-09-22 20:21:34 +0000261 LinkLibraries(argv[0], Composite.get(), Libraries, LibPaths, Verbose, Native);
John Criswell71478b72003-09-19 20:24:23 +0000262
Chris Lattner10970eb2003-04-19 22:44:38 +0000263 // Link in all of the libraries next...
Chris Lattnere7fca512002-01-24 19:12:12 +0000264
Chris Lattnerad202a02002-04-08 00:14:58 +0000265 //
John Criswelldc0de4f2003-09-18 16:22:26 +0000266 // Create the output file.
Chris Lattnerad202a02002-04-08 00:14:58 +0000267 //
Chris Lattner10970eb2003-04-19 22:44:38 +0000268 std::string RealBytecodeOutput = OutputFilename;
269 if (!LinkAsLibrary) RealBytecodeOutput += ".bc";
270 std::ofstream Out(RealBytecodeOutput.c_str());
271 if (!Out.good())
272 return PrintAndReturn(argv[0], "error opening '" + RealBytecodeOutput +
273 "' for writing!");
Chris Lattnere7fca512002-01-24 19:12:12 +0000274
John Criswelldc0de4f2003-09-18 16:22:26 +0000275 //
276 // Ensure that the bytecode file gets removed from the disk if we get a
277 // SIGINT signal.
278 //
Chris Lattner10970eb2003-04-19 22:44:38 +0000279 RemoveFileOnSignal(RealBytecodeOutput);
Chris Lattner76d12292002-04-18 19:55:25 +0000280
John Criswelldc0de4f2003-09-18 16:22:26 +0000281 //
282 // Generate the bytecode file.
283 //
Chris Lattner6e271232003-09-22 20:21:34 +0000284 if (GenerateBytecode (Composite.get(), Strip, !NoInternalize, &Out)) {
John Criswelldc0de4f2003-09-18 16:22:26 +0000285 Out.close();
286 return PrintAndReturn(argv[0], "error generating bytcode");
287 }
288
289 //
290 // Close the bytecode file.
291 //
Chris Lattnere7fca512002-01-24 19:12:12 +0000292 Out.close();
293
John Criswelldc0de4f2003-09-18 16:22:26 +0000294 //
295 // If we are not linking a library, generate either a native executable
296 // or a JIT shell script, depending upon what the user wants.
297 //
Chris Lattner10970eb2003-04-19 22:44:38 +0000298 if (!LinkAsLibrary) {
John Criswelldabaf7d2003-09-16 21:27:35 +0000299 //
300 // If the user wants to generate a native executable, compile it from the
301 // bytecode file.
302 //
303 // Otherwise, create a script that will run the bytecode through the JIT.
304 //
Chris Lattner6e271232003-09-22 20:21:34 +0000305 if (Native) {
John Criswelldc0de4f2003-09-18 16:22:26 +0000306 // Name of the Assembly Language output file
307 std::string AssemblyFile = OutputFilename + ".s";
308
John Criswelldabaf7d2003-09-16 21:27:35 +0000309 //
John Criswelldc0de4f2003-09-18 16:22:26 +0000310 // Mark the output files for removal if we get an interrupt.
John Criswelldabaf7d2003-09-16 21:27:35 +0000311 //
John Criswelldc0de4f2003-09-18 16:22:26 +0000312 RemoveFileOnSignal (AssemblyFile);
313 RemoveFileOnSignal (OutputFilename);
John Criswelldabaf7d2003-09-16 21:27:35 +0000314
315 //
John Criswell83ca6ec2003-09-17 19:04:22 +0000316 // Determine the locations of the llc and gcc programs.
317 //
318 std::string llc=FindExecutable ("llc", argv[0]);
319 std::string gcc=FindExecutable ("gcc", argv[0]);
320 if (llc.empty())
John Criswell83ca6ec2003-09-17 19:04:22 +0000321 return PrintAndReturn (argv[0], "Failed to find llc");
John Criswell83ca6ec2003-09-17 19:04:22 +0000322
323 if (gcc.empty())
John Criswell83ca6ec2003-09-17 19:04:22 +0000324 return PrintAndReturn (argv[0], "Failed to find gcc");
John Criswell83ca6ec2003-09-17 19:04:22 +0000325
326 //
John Criswelldc0de4f2003-09-18 16:22:26 +0000327 // Generate an assembly language file for the bytecode.
John Criswelldabaf7d2003-09-16 21:27:35 +0000328 //
John Criswell71478b72003-09-19 20:24:23 +0000329 if (Verbose) std::cout << "Generating Assembly Code\n";
Chris Lattner6e271232003-09-22 20:21:34 +0000330 GenerateAssembly(AssemblyFile, RealBytecodeOutput, llc, envp);
John Criswell71478b72003-09-19 20:24:23 +0000331 if (Verbose) std::cout << "Generating Native Code\n";
Chris Lattner6e271232003-09-22 20:21:34 +0000332 GenerateNative(OutputFilename, AssemblyFile, Libraries, LibPaths,
333 gcc, envp);
John Criswelldabaf7d2003-09-16 21:27:35 +0000334
335 //
John Criswelldc0de4f2003-09-18 16:22:26 +0000336 // Remove the assembly language file.
John Criswelldabaf7d2003-09-16 21:27:35 +0000337 //
John Criswelldc0de4f2003-09-18 16:22:26 +0000338 removeFile (AssemblyFile);
Chris Lattner6e271232003-09-22 20:21:34 +0000339 } else {
John Criswelldabaf7d2003-09-16 21:27:35 +0000340 // Output the script to start the program...
341 std::ofstream Out2(OutputFilename.c_str());
342 if (!Out2.good())
343 return PrintAndReturn(argv[0], "error opening '" + OutputFilename +
344 "' for writing!");
345 Out2 << "#!/bin/sh\nlli -q $0.bc $*\n";
346 Out2.close();
347 }
Chris Lattnere7fca512002-01-24 19:12:12 +0000348
Chris Lattner10970eb2003-04-19 22:44:38 +0000349 // Make the script executable...
John Criswelld35b5b52003-09-02 20:17:20 +0000350 MakeFileExecutable (OutputFilename);
Misha Brukmanc1fdca82003-08-20 20:38:15 +0000351
John Criswell22edc392003-09-02 21:11:22 +0000352 // Make the bytecode file readable and directly executable in LLEE as well
John Criswelld35b5b52003-09-02 20:17:20 +0000353 MakeFileExecutable (RealBytecodeOutput);
John Criswell22edc392003-09-02 21:11:22 +0000354 MakeFileReadable (RealBytecodeOutput);
Chris Lattner10970eb2003-04-19 22:44:38 +0000355 }
Chris Lattnere7fca512002-01-24 19:12:12 +0000356
357 return 0;
358}