blob: 5a1b26100a9d7551d5d59422eb6612886db7cb89 [file] [log] [blame]
Chris Lattner1d496172003-04-19 22:44:38 +00001//===- gccld.cpp - LLVM 'ld' compatible linker ----------------------------===//
John Criswell09344dc2003-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 Lattner5ff2e052002-01-24 19:12:12 +00009//
10// This utility is intended to be compatible with GCC, and follows standard
Chris Lattner1d496172003-04-19 22:44:38 +000011// system 'ld' conventions. As such, the default output file is ./a.out.
Chris Lattner5ff2e052002-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 Gaeke5bfa37f2003-05-23 20:27:07 +000019// I'm not too worried about this.
Chris Lattner5ff2e052002-01-24 19:12:12 +000020//
21//===----------------------------------------------------------------------===//
22
Chris Lattner65d93e22003-09-22 20:21:34 +000023#include "gccld.h"
Chris Lattner5ff2e052002-01-24 19:12:12 +000024#include "llvm/Module.h"
Chris Lattner3b08c2f2002-04-08 00:14:58 +000025#include "llvm/PassManager.h"
26#include "llvm/Bytecode/Reader.h"
27#include "llvm/Bytecode/WriteBytecodePass.h"
Chris Lattnerd571e2a2003-04-24 19:13:02 +000028#include "llvm/Target/TargetData.h"
Chris Lattner35c45412002-07-23 22:04:43 +000029#include "llvm/Transforms/IPO.h"
Chris Lattner35c45412002-07-23 22:04:43 +000030#include "llvm/Transforms/Scalar.h"
Misha Brukman21663632003-09-30 17:59:25 +000031#include "llvm/Transforms/Utils/Linker.h"
Chris Lattner5ff2e052002-01-24 19:12:12 +000032#include "Support/CommandLine.h"
Misha Brukman21663632003-09-30 17:59:25 +000033#include "Support/FileUtilities.h"
Chris Lattnerc065ad82002-04-18 19:55:25 +000034#include "Support/Signals.h"
Misha Brukman21663632003-09-30 17:59:25 +000035#include "Support/SystemUtils.h"
Chris Lattner5ff2e052002-01-24 19:12:12 +000036#include <fstream>
37#include <memory>
Chris Lattner5ff2e052002-01-24 19:12:12 +000038
Brian Gaeke960707c2003-11-11 22:41:34 +000039using namespace llvm;
40
Chris Lattner2b3a5db2003-04-18 23:01:25 +000041namespace {
42 cl::list<std::string>
43 InputFilenames(cl::Positional, cl::desc("<input bytecode files>"),
44 cl::OneOrMore);
Chris Lattnerf5cad152002-07-22 02:10:13 +000045
Chris Lattner2b3a5db2003-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 Lattnerf5cad152002-07-22 02:10:13 +000049
Chris Lattner2b3a5db2003-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 Lattnerf5cad152002-07-22 02:10:13 +000056
Chris Lattner2b3a5db2003-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 Lattnerf5cad152002-07-22 02:10:13 +000060
Chris Lattner2b3a5db2003-04-18 23:01:25 +000061 cl::opt<bool>
62 Strip("s", cl::desc("Strip symbol info from executable"));
Chris Lattnerf5cad152002-07-22 02:10:13 +000063
Chris Lattner2b3a5db2003-04-18 23:01:25 +000064 cl::opt<bool>
65 NoInternalize("disable-internalize",
66 cl::desc("Do not mark all symbols as internal"));
Chris Lattnerc17fe1c2003-11-05 06:05:21 +000067 cl::alias
Chris Lattnerb4d99212003-08-22 19:18:45 +000068 ExportDynamic("export-dynamic", cl::desc("Alias for -disable-internalize"),
69 cl::aliasopt(NoInternalize));
Chris Lattner602d2092003-04-18 23:38:22 +000070
Chris Lattner1d496172003-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 Lattnerc17fe1c2003-11-05 06:05:21 +000074 cl::alias
75 Relink("r", cl::desc("Alias for -link-as-library"),
76 cl::aliasopt(LinkAsLibrary));
Chris Lattner1d496172003-04-19 22:44:38 +000077
John Criswell1997a342003-09-16 21:27:35 +000078 cl::opt<bool>
Chris Lattner65d93e22003-09-22 20:21:34 +000079 Native("native",
80 cl::desc("Generate a native binary instead of a shell script"));
John Criswell1997a342003-09-16 21:27:35 +000081
John Criswell8ecc3022003-09-18 16:22:26 +000082 // Compatibility options that are ignored but supported by LD
Chris Lattner602d2092003-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"));
Chris Lattner2b3a5db2003-04-18 23:01:25 +000089}
Chris Lattner5ff2e052002-01-24 19:12:12 +000090
Brian Gaeke960707c2003-11-11 22:41:34 +000091namespace llvm {
92
Chris Lattner1d496172003-04-19 22:44:38 +000093//
John Criswellb533bde2003-09-19 20:24:23 +000094// Function: PrintAndReturn ()
95//
96// Description:
97// Prints a message (usually error message) to standard error (stderr) and
98// returns a value usable for an exit status.
99//
100// Inputs:
101// progname - The name of the program (i.e. argv[0]).
102// Message - The message to print to standard error.
103// Extra - Extra information to print between the program name and thei
104// message. It is optional.
105//
106// Outputs:
107// None.
108//
109// Return value:
110// Returns a value that can be used as the exit status (i.e. for exit()).
111//
112int
113PrintAndReturn (const char *progname,
114 const std::string &Message,
115 const std::string &Extra)
116{
117 std::cerr << progname << Extra << ": " << Message << "\n";
118 return 1;
Chris Lattner1d496172003-04-19 22:44:38 +0000119}
120
John Criswellb533bde2003-09-19 20:24:23 +0000121//
122//
123// Function: CopyEnv()
124//
125// Description:
126// This function takes an array of environment variables and makes a
127// copy of it. This copy can then be manipulated any way the caller likes
128// without affecting the process's real environment.
129//
130// Inputs:
131// envp - An array of C strings containing an environment.
132//
133// Outputs:
134// None.
135//
136// Return value:
137// NULL - An error occurred.
138//
139// Otherwise, a pointer to a new array of C strings is returned. Every string
140// in the array is a duplicate of the one in the original array (i.e. we do
141// not copy the char *'s from one array to another).
142//
Misha Brukman21663632003-09-30 17:59:25 +0000143char ** CopyEnv(char ** const envp) {
John Criswellb533bde2003-09-19 20:24:23 +0000144 // Count the number of entries in the old list;
Chris Lattner65d93e22003-09-22 20:21:34 +0000145 unsigned entries; // The number of entries in the old environment list
John Criswellb533bde2003-09-19 20:24:23 +0000146 for (entries = 0; envp[entries] != NULL; entries++)
147 {
148 ;
Chris Lattnera2d35042003-05-13 22:14:13 +0000149 }
150
John Criswellb533bde2003-09-19 20:24:23 +0000151 // Add one more entry for the NULL pointer that ends the list.
John Criswellb533bde2003-09-19 20:24:23 +0000152 ++entries;
Chris Lattner1d496172003-04-19 22:44:38 +0000153
John Criswellb533bde2003-09-19 20:24:23 +0000154 // If there are no entries at all, just return NULL.
John Criswellb533bde2003-09-19 20:24:23 +0000155 if (entries == 0)
John Criswellb533bde2003-09-19 20:24:23 +0000156 return NULL;
Chris Lattner1d496172003-04-19 22:44:38 +0000157
John Criswellb533bde2003-09-19 20:24:23 +0000158 // Allocate a new environment list.
Chris Lattner65d93e22003-09-22 20:21:34 +0000159 char **newenv;
John Criswellb533bde2003-09-19 20:24:23 +0000160 if ((newenv = new (char *) [entries]) == NULL)
John Criswellb533bde2003-09-19 20:24:23 +0000161 return NULL;
Chris Lattner1d496172003-04-19 22:44:38 +0000162
John Criswellb533bde2003-09-19 20:24:23 +0000163 // Make a copy of the list. Don't forget the NULL that ends the list.
John Criswellb533bde2003-09-19 20:24:23 +0000164 entries = 0;
Misha Brukman21663632003-09-30 17:59:25 +0000165 while (envp[entries] != NULL) {
John Criswellb533bde2003-09-19 20:24:23 +0000166 newenv[entries] = new char[strlen (envp[entries]) + 1];
167 strcpy (newenv[entries], envp[entries]);
168 ++entries;
169 }
170 newenv[entries] = NULL;
Chris Lattner1d496172003-04-19 22:44:38 +0000171
John Criswellb533bde2003-09-19 20:24:23 +0000172 return newenv;
173}
174
175
176//
177// Function: RemoveEnv()
178//
179// Description:
180// Remove the specified environment variable from the environment array.
181//
182// Inputs:
183// name - The name of the variable to remove. It cannot be NULL.
184// envp - The array of environment variables. It cannot be NULL.
185//
186// Outputs:
187// envp - The pointer to the specified variable name is removed.
188//
189// Return value:
190// None.
191//
192// Notes:
193// This is mainly done because functions to remove items from the environment
194// are not available across all platforms. In particular, Solaris does not
195// seem to have an unsetenv() function or a setenv() function (or they are
196// undocumented if they do exist).
197//
Misha Brukman21663632003-09-30 17:59:25 +0000198void RemoveEnv(const char * name, char ** const envp) {
Chris Lattner65d93e22003-09-22 20:21:34 +0000199 for (unsigned index=0; envp[index] != NULL; index++) {
John Criswellb533bde2003-09-19 20:24:23 +0000200 // Find the first equals sign in the array and make it an EOS character.
Chris Lattner65d93e22003-09-22 20:21:34 +0000201 char *p = strchr (envp[index], '=');
John Criswellb533bde2003-09-19 20:24:23 +0000202 if (p == NULL)
John Criswellb533bde2003-09-19 20:24:23 +0000203 continue;
John Criswellb533bde2003-09-19 20:24:23 +0000204 else
John Criswellb533bde2003-09-19 20:24:23 +0000205 *p = '\0';
John Criswellb533bde2003-09-19 20:24:23 +0000206
John Criswellb533bde2003-09-19 20:24:23 +0000207 // Compare the two strings. If they are equal, zap this string.
208 // Otherwise, restore it.
Misha Brukman21663632003-09-30 17:59:25 +0000209 if (!strcmp(name, envp[index]))
John Criswellb533bde2003-09-19 20:24:23 +0000210 *envp[index] = '\0';
John Criswellb533bde2003-09-19 20:24:23 +0000211 else
John Criswellb533bde2003-09-19 20:24:23 +0000212 *p = '=';
Chris Lattner1d496172003-04-19 22:44:38 +0000213 }
John Criswellb533bde2003-09-19 20:24:23 +0000214
215 return;
Chris Lattner1d496172003-04-19 22:44:38 +0000216}
217
Brian Gaeke960707c2003-11-11 22:41:34 +0000218} // End llvm namespace
Chris Lattner1d496172003-04-19 22:44:38 +0000219
Misha Brukman21663632003-09-30 17:59:25 +0000220int main(int argc, char **argv, char **envp) {
Chris Lattnerf5cad152002-07-22 02:10:13 +0000221 cl::ParseCommandLineOptions(argc, argv, " llvm linker for GCC\n");
Chris Lattner5ff2e052002-01-24 19:12:12 +0000222
Brian Gaeke1d9515b2003-11-05 22:13:00 +0000223 std::string ModuleID ("gccld-output");
224 std::auto_ptr<Module> Composite(new Module(ModuleID));
Chris Lattner5ff2e052002-01-24 19:12:12 +0000225
Brian Gaeke5bfa37f2003-05-23 20:27:07 +0000226 // We always look first in the current directory when searching for libraries.
227 LibPaths.insert(LibPaths.begin(), ".");
228
Misha Brukman8b2bd4e2003-10-10 17:57:28 +0000229 // If the user specified an extra search path in their environment, respect
230 // it.
Chris Lattnerda3bc212003-04-21 19:53:24 +0000231 if (char *SearchPath = getenv("LLVM_LIB_SEARCH_PATH"))
232 LibPaths.push_back(SearchPath);
233
Chris Lattner4b462c02003-04-19 23:07:33 +0000234 // Remove any consecutive duplicates of the same library...
235 Libraries.erase(std::unique(Libraries.begin(), Libraries.end()),
236 Libraries.end());
237
John Criswellb533bde2003-09-19 20:24:23 +0000238 // Link in all of the files
Brian Gaeke484f2c72003-09-30 14:03:48 +0000239 if (LinkFiles(argv[0], Composite.get(), InputFilenames, Verbose))
240 return 1; // Error already printed
Chris Lattnercbf2aeb2003-11-03 17:27:17 +0000241
242 if (!LinkAsLibrary)
243 LinkLibraries(argv[0], Composite.get(), Libraries, LibPaths,
244 Verbose, Native);
John Criswellb533bde2003-09-19 20:24:23 +0000245
Chris Lattner1d496172003-04-19 22:44:38 +0000246 // Link in all of the libraries next...
Chris Lattner5ff2e052002-01-24 19:12:12 +0000247
John Criswell8ecc3022003-09-18 16:22:26 +0000248 // Create the output file.
Chris Lattner1d496172003-04-19 22:44:38 +0000249 std::string RealBytecodeOutput = OutputFilename;
250 if (!LinkAsLibrary) RealBytecodeOutput += ".bc";
251 std::ofstream Out(RealBytecodeOutput.c_str());
252 if (!Out.good())
253 return PrintAndReturn(argv[0], "error opening '" + RealBytecodeOutput +
254 "' for writing!");
Chris Lattner5ff2e052002-01-24 19:12:12 +0000255
John Criswell8ecc3022003-09-18 16:22:26 +0000256 // Ensure that the bytecode file gets removed from the disk if we get a
257 // SIGINT signal.
Chris Lattner1d496172003-04-19 22:44:38 +0000258 RemoveFileOnSignal(RealBytecodeOutput);
Chris Lattnerc065ad82002-04-18 19:55:25 +0000259
John Criswell8ecc3022003-09-18 16:22:26 +0000260 // Generate the bytecode file.
Misha Brukman21663632003-09-30 17:59:25 +0000261 if (GenerateBytecode(Composite.get(), Strip, !NoInternalize, &Out)) {
John Criswell8ecc3022003-09-18 16:22:26 +0000262 Out.close();
Brian Gaeke1d9515b2003-11-05 22:13:00 +0000263 return PrintAndReturn(argv[0], "error generating bytecode");
John Criswell8ecc3022003-09-18 16:22:26 +0000264 }
265
John Criswell8ecc3022003-09-18 16:22:26 +0000266 // Close the bytecode file.
Chris Lattner5ff2e052002-01-24 19:12:12 +0000267 Out.close();
268
John Criswell8ecc3022003-09-18 16:22:26 +0000269 // If we are not linking a library, generate either a native executable
270 // or a JIT shell script, depending upon what the user wants.
Chris Lattner1d496172003-04-19 22:44:38 +0000271 if (!LinkAsLibrary) {
John Criswell1997a342003-09-16 21:27:35 +0000272 // If the user wants to generate a native executable, compile it from the
273 // bytecode file.
274 //
275 // Otherwise, create a script that will run the bytecode through the JIT.
Chris Lattner65d93e22003-09-22 20:21:34 +0000276 if (Native) {
John Criswell8ecc3022003-09-18 16:22:26 +0000277 // Name of the Assembly Language output file
278 std::string AssemblyFile = OutputFilename + ".s";
279
John Criswell8ecc3022003-09-18 16:22:26 +0000280 // Mark the output files for removal if we get an interrupt.
Misha Brukman21663632003-09-30 17:59:25 +0000281 RemoveFileOnSignal(AssemblyFile);
282 RemoveFileOnSignal(OutputFilename);
John Criswell1997a342003-09-16 21:27:35 +0000283
John Criswell0217b1b2003-09-17 19:04:22 +0000284 // Determine the locations of the llc and gcc programs.
Misha Brukman21663632003-09-30 17:59:25 +0000285 std::string llc = FindExecutable("llc", argv[0]);
286 std::string gcc = FindExecutable("gcc", argv[0]);
John Criswell0217b1b2003-09-17 19:04:22 +0000287 if (llc.empty())
Misha Brukman21663632003-09-30 17:59:25 +0000288 return PrintAndReturn(argv[0], "Failed to find llc");
John Criswell0217b1b2003-09-17 19:04:22 +0000289
290 if (gcc.empty())
Misha Brukman21663632003-09-30 17:59:25 +0000291 return PrintAndReturn(argv[0], "Failed to find gcc");
John Criswell0217b1b2003-09-17 19:04:22 +0000292
John Criswell8ecc3022003-09-18 16:22:26 +0000293 // Generate an assembly language file for the bytecode.
John Criswellb533bde2003-09-19 20:24:23 +0000294 if (Verbose) std::cout << "Generating Assembly Code\n";
Chris Lattner65d93e22003-09-22 20:21:34 +0000295 GenerateAssembly(AssemblyFile, RealBytecodeOutput, llc, envp);
John Criswellb533bde2003-09-19 20:24:23 +0000296 if (Verbose) std::cout << "Generating Native Code\n";
Chris Lattner65d93e22003-09-22 20:21:34 +0000297 GenerateNative(OutputFilename, AssemblyFile, Libraries, LibPaths,
298 gcc, envp);
John Criswell1997a342003-09-16 21:27:35 +0000299
John Criswell8ecc3022003-09-18 16:22:26 +0000300 // Remove the assembly language file.
John Criswell8ecc3022003-09-18 16:22:26 +0000301 removeFile (AssemblyFile);
Chris Lattner65d93e22003-09-22 20:21:34 +0000302 } else {
John Criswell1997a342003-09-16 21:27:35 +0000303 // Output the script to start the program...
304 std::ofstream Out2(OutputFilename.c_str());
305 if (!Out2.good())
306 return PrintAndReturn(argv[0], "error opening '" + OutputFilename +
307 "' for writing!");
Brian Gaeke275811122003-10-24 20:00:06 +0000308 Out2 << "#!/bin/sh\nlli $0.bc $*\n";
John Criswell1997a342003-09-16 21:27:35 +0000309 Out2.close();
310 }
Chris Lattner5ff2e052002-01-24 19:12:12 +0000311
Chris Lattner1d496172003-04-19 22:44:38 +0000312 // Make the script executable...
Misha Brukman21663632003-09-30 17:59:25 +0000313 MakeFileExecutable(OutputFilename);
Misha Brukman90869942003-08-20 20:38:15 +0000314
John Criswella3ce8b42003-09-02 21:11:22 +0000315 // Make the bytecode file readable and directly executable in LLEE as well
Misha Brukman21663632003-09-30 17:59:25 +0000316 MakeFileExecutable(RealBytecodeOutput);
317 MakeFileReadable(RealBytecodeOutput);
Chris Lattner1d496172003-04-19 22:44:38 +0000318 }
Chris Lattner5ff2e052002-01-24 19:12:12 +0000319
320 return 0;
321}