blob: 4c32c18089a977b27d4bfe9ab5298507690fd44f [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
Misha Brukmand16a0372003-11-20 06:21:54 +000093/// PrintAndReturn - Prints a message to standard error and returns a value
94/// usable for an exit status.
95///
96/// Inputs:
97/// progname - The name of the program (i.e. argv[0]).
98/// Message - The message to print to standard error.
99/// Extra - Extra information to print between the program name and thei
100/// message. It is optional.
101///
102/// Return value:
103/// Returns a value that can be used as the exit status (i.e. for exit()).
104///
John Criswellb533bde2003-09-19 20:24:23 +0000105int
Misha Brukmand16a0372003-11-20 06:21:54 +0000106PrintAndReturn(const char *progname,
107 const std::string &Message,
108 const std::string &Extra)
John Criswellb533bde2003-09-19 20:24:23 +0000109{
110 std::cerr << progname << Extra << ": " << Message << "\n";
111 return 1;
Chris Lattner1d496172003-04-19 22:44:38 +0000112}
113
Misha Brukmand16a0372003-11-20 06:21:54 +0000114/// CopyEnv - This function takes an array of environment variables and makes a
115/// copy of it. This copy can then be manipulated any way the caller likes
116/// without affecting the process's real environment.
117///
118/// Inputs:
119/// envp - An array of C strings containing an environment.
120///
121/// Return value:
122/// NULL - An error occurred.
123///
124/// Otherwise, a pointer to a new array of C strings is returned. Every string
125/// in the array is a duplicate of the one in the original array (i.e. we do
126/// not copy the char *'s from one array to another).
127///
Misha Brukman21663632003-09-30 17:59:25 +0000128char ** CopyEnv(char ** const envp) {
John Criswellb533bde2003-09-19 20:24:23 +0000129 // Count the number of entries in the old list;
Chris Lattner65d93e22003-09-22 20:21:34 +0000130 unsigned entries; // The number of entries in the old environment list
John Criswellb533bde2003-09-19 20:24:23 +0000131 for (entries = 0; envp[entries] != NULL; entries++)
Chris Lattner982b2852003-11-29 19:45:47 +0000132 /*empty*/;
Chris Lattnera2d35042003-05-13 22:14:13 +0000133
John Criswellb533bde2003-09-19 20:24:23 +0000134 // Add one more entry for the NULL pointer that ends the list.
John Criswellb533bde2003-09-19 20:24:23 +0000135 ++entries;
Chris Lattner1d496172003-04-19 22:44:38 +0000136
John Criswellb533bde2003-09-19 20:24:23 +0000137 // If there are no entries at all, just return NULL.
John Criswellb533bde2003-09-19 20:24:23 +0000138 if (entries == 0)
John Criswellb533bde2003-09-19 20:24:23 +0000139 return NULL;
Chris Lattner1d496172003-04-19 22:44:38 +0000140
John Criswellb533bde2003-09-19 20:24:23 +0000141 // Allocate a new environment list.
Chris Lattner982b2852003-11-29 19:45:47 +0000142 char **newenv = new char* [entries];
143 if ((newenv = new char* [entries]) == NULL)
John Criswellb533bde2003-09-19 20:24:23 +0000144 return NULL;
Chris Lattner1d496172003-04-19 22:44:38 +0000145
John Criswellb533bde2003-09-19 20:24:23 +0000146 // Make a copy of the list. Don't forget the NULL that ends the list.
John Criswellb533bde2003-09-19 20:24:23 +0000147 entries = 0;
Misha Brukman21663632003-09-30 17:59:25 +0000148 while (envp[entries] != NULL) {
John Criswellb533bde2003-09-19 20:24:23 +0000149 newenv[entries] = new char[strlen (envp[entries]) + 1];
150 strcpy (newenv[entries], envp[entries]);
151 ++entries;
152 }
153 newenv[entries] = NULL;
Chris Lattner1d496172003-04-19 22:44:38 +0000154
John Criswellb533bde2003-09-19 20:24:23 +0000155 return newenv;
156}
157
158
Misha Brukmand16a0372003-11-20 06:21:54 +0000159/// RemoveEnv - Remove the specified environment variable from the environment
160/// array.
161///
162/// Inputs:
163/// name - The name of the variable to remove. It cannot be NULL.
164/// envp - The array of environment variables. It cannot be NULL.
165///
166/// Notes:
167/// This is mainly done because functions to remove items from the environment
168/// are not available across all platforms. In particular, Solaris does not
169/// seem to have an unsetenv() function or a setenv() function (or they are
170/// undocumented if they do exist).
171///
Misha Brukman21663632003-09-30 17:59:25 +0000172void RemoveEnv(const char * name, char ** const envp) {
Chris Lattner65d93e22003-09-22 20:21:34 +0000173 for (unsigned index=0; envp[index] != NULL; index++) {
John Criswellb533bde2003-09-19 20:24:23 +0000174 // Find the first equals sign in the array and make it an EOS character.
Chris Lattner65d93e22003-09-22 20:21:34 +0000175 char *p = strchr (envp[index], '=');
John Criswellb533bde2003-09-19 20:24:23 +0000176 if (p == NULL)
John Criswellb533bde2003-09-19 20:24:23 +0000177 continue;
John Criswellb533bde2003-09-19 20:24:23 +0000178 else
John Criswellb533bde2003-09-19 20:24:23 +0000179 *p = '\0';
John Criswellb533bde2003-09-19 20:24:23 +0000180
John Criswellb533bde2003-09-19 20:24:23 +0000181 // Compare the two strings. If they are equal, zap this string.
182 // Otherwise, restore it.
Misha Brukman21663632003-09-30 17:59:25 +0000183 if (!strcmp(name, envp[index]))
John Criswellb533bde2003-09-19 20:24:23 +0000184 *envp[index] = '\0';
John Criswellb533bde2003-09-19 20:24:23 +0000185 else
John Criswellb533bde2003-09-19 20:24:23 +0000186 *p = '=';
Chris Lattner1d496172003-04-19 22:44:38 +0000187 }
John Criswellb533bde2003-09-19 20:24:23 +0000188
189 return;
Chris Lattner1d496172003-04-19 22:44:38 +0000190}
191
Brian Gaeke960707c2003-11-11 22:41:34 +0000192} // End llvm namespace
Chris Lattner1d496172003-04-19 22:44:38 +0000193
Misha Brukman21663632003-09-30 17:59:25 +0000194int main(int argc, char **argv, char **envp) {
Chris Lattnerf5cad152002-07-22 02:10:13 +0000195 cl::ParseCommandLineOptions(argc, argv, " llvm linker for GCC\n");
Chris Lattner5ff2e052002-01-24 19:12:12 +0000196
Misha Brukmand16a0372003-11-20 06:21:54 +0000197 std::string ModuleID("gccld-output");
Brian Gaeke1d9515b2003-11-05 22:13:00 +0000198 std::auto_ptr<Module> Composite(new Module(ModuleID));
Chris Lattner5ff2e052002-01-24 19:12:12 +0000199
Brian Gaeke5bfa37f2003-05-23 20:27:07 +0000200 // We always look first in the current directory when searching for libraries.
201 LibPaths.insert(LibPaths.begin(), ".");
202
Misha Brukman8b2bd4e2003-10-10 17:57:28 +0000203 // If the user specified an extra search path in their environment, respect
204 // it.
Chris Lattnerda3bc212003-04-21 19:53:24 +0000205 if (char *SearchPath = getenv("LLVM_LIB_SEARCH_PATH"))
206 LibPaths.push_back(SearchPath);
207
Chris Lattner4b462c02003-04-19 23:07:33 +0000208 // Remove any consecutive duplicates of the same library...
209 Libraries.erase(std::unique(Libraries.begin(), Libraries.end()),
210 Libraries.end());
211
John Criswellb533bde2003-09-19 20:24:23 +0000212 // Link in all of the files
Brian Gaeke484f2c72003-09-30 14:03:48 +0000213 if (LinkFiles(argv[0], Composite.get(), InputFilenames, Verbose))
214 return 1; // Error already printed
Chris Lattnercbf2aeb2003-11-03 17:27:17 +0000215
216 if (!LinkAsLibrary)
217 LinkLibraries(argv[0], Composite.get(), Libraries, LibPaths,
218 Verbose, Native);
John Criswellb533bde2003-09-19 20:24:23 +0000219
Chris Lattner1d496172003-04-19 22:44:38 +0000220 // Link in all of the libraries next...
Chris Lattner5ff2e052002-01-24 19:12:12 +0000221
John Criswell8ecc3022003-09-18 16:22:26 +0000222 // Create the output file.
Chris Lattner1d496172003-04-19 22:44:38 +0000223 std::string RealBytecodeOutput = OutputFilename;
224 if (!LinkAsLibrary) RealBytecodeOutput += ".bc";
225 std::ofstream Out(RealBytecodeOutput.c_str());
226 if (!Out.good())
227 return PrintAndReturn(argv[0], "error opening '" + RealBytecodeOutput +
228 "' for writing!");
Chris Lattner5ff2e052002-01-24 19:12:12 +0000229
John Criswell8ecc3022003-09-18 16:22:26 +0000230 // Ensure that the bytecode file gets removed from the disk if we get a
231 // SIGINT signal.
Chris Lattner1d496172003-04-19 22:44:38 +0000232 RemoveFileOnSignal(RealBytecodeOutput);
Chris Lattnerc065ad82002-04-18 19:55:25 +0000233
John Criswell8ecc3022003-09-18 16:22:26 +0000234 // Generate the bytecode file.
Misha Brukman21663632003-09-30 17:59:25 +0000235 if (GenerateBytecode(Composite.get(), Strip, !NoInternalize, &Out)) {
John Criswell8ecc3022003-09-18 16:22:26 +0000236 Out.close();
Brian Gaeke1d9515b2003-11-05 22:13:00 +0000237 return PrintAndReturn(argv[0], "error generating bytecode");
John Criswell8ecc3022003-09-18 16:22:26 +0000238 }
239
John Criswell8ecc3022003-09-18 16:22:26 +0000240 // Close the bytecode file.
Chris Lattner5ff2e052002-01-24 19:12:12 +0000241 Out.close();
242
John Criswell8ecc3022003-09-18 16:22:26 +0000243 // If we are not linking a library, generate either a native executable
244 // or a JIT shell script, depending upon what the user wants.
Chris Lattner1d496172003-04-19 22:44:38 +0000245 if (!LinkAsLibrary) {
John Criswell1997a342003-09-16 21:27:35 +0000246 // If the user wants to generate a native executable, compile it from the
247 // bytecode file.
248 //
249 // Otherwise, create a script that will run the bytecode through the JIT.
Chris Lattner65d93e22003-09-22 20:21:34 +0000250 if (Native) {
John Criswell8ecc3022003-09-18 16:22:26 +0000251 // Name of the Assembly Language output file
252 std::string AssemblyFile = OutputFilename + ".s";
253
John Criswell8ecc3022003-09-18 16:22:26 +0000254 // Mark the output files for removal if we get an interrupt.
Misha Brukman21663632003-09-30 17:59:25 +0000255 RemoveFileOnSignal(AssemblyFile);
256 RemoveFileOnSignal(OutputFilename);
John Criswell1997a342003-09-16 21:27:35 +0000257
John Criswell0217b1b2003-09-17 19:04:22 +0000258 // Determine the locations of the llc and gcc programs.
Misha Brukman21663632003-09-30 17:59:25 +0000259 std::string llc = FindExecutable("llc", argv[0]);
260 std::string gcc = FindExecutable("gcc", argv[0]);
John Criswell0217b1b2003-09-17 19:04:22 +0000261 if (llc.empty())
Misha Brukman21663632003-09-30 17:59:25 +0000262 return PrintAndReturn(argv[0], "Failed to find llc");
John Criswell0217b1b2003-09-17 19:04:22 +0000263
264 if (gcc.empty())
Misha Brukman21663632003-09-30 17:59:25 +0000265 return PrintAndReturn(argv[0], "Failed to find gcc");
John Criswell0217b1b2003-09-17 19:04:22 +0000266
John Criswell8ecc3022003-09-18 16:22:26 +0000267 // Generate an assembly language file for the bytecode.
John Criswellb533bde2003-09-19 20:24:23 +0000268 if (Verbose) std::cout << "Generating Assembly Code\n";
Chris Lattner65d93e22003-09-22 20:21:34 +0000269 GenerateAssembly(AssemblyFile, RealBytecodeOutput, llc, envp);
John Criswellb533bde2003-09-19 20:24:23 +0000270 if (Verbose) std::cout << "Generating Native Code\n";
Chris Lattner65d93e22003-09-22 20:21:34 +0000271 GenerateNative(OutputFilename, AssemblyFile, Libraries, LibPaths,
272 gcc, envp);
John Criswell1997a342003-09-16 21:27:35 +0000273
John Criswell8ecc3022003-09-18 16:22:26 +0000274 // Remove the assembly language file.
John Criswell8ecc3022003-09-18 16:22:26 +0000275 removeFile (AssemblyFile);
Chris Lattner65d93e22003-09-22 20:21:34 +0000276 } else {
John Criswell1997a342003-09-16 21:27:35 +0000277 // Output the script to start the program...
278 std::ofstream Out2(OutputFilename.c_str());
279 if (!Out2.good())
280 return PrintAndReturn(argv[0], "error opening '" + OutputFilename +
281 "' for writing!");
Misha Brukman1df12622003-11-20 19:08:42 +0000282 Out2 << "#!/bin/sh\nlli \\\n";
283 // gcc accepts -l<lib> and implicitly searches /lib and /usr/lib.
284 LibPaths.push_back("/lib");
285 LibPaths.push_back("/usr/lib");
Misha Brukmanc10bf392003-11-24 05:31:57 +0000286 LibPaths.push_back("/usr/X11R6/lib");
Misha Brukman1df12622003-11-20 19:08:42 +0000287 // We don't need to link in libc! In fact, /usr/lib/libc.so may not be a
288 // shared object at all! See RH 8: plain text.
289 std::vector<std::string>::iterator libc =
290 std::find(Libraries.begin(), Libraries.end(), "c");
291 if (libc != Libraries.end()) Libraries.erase(libc);
292 // List all the shared object (native) libraries this executable will need
293 // on the command line, so that we don't have to do this manually!
294 for (std::vector<std::string>::iterator i = Libraries.begin(),
295 e = Libraries.end(); i != e; ++i) {
296 std::string FullLibraryPath = FindLib(*i, LibPaths, true);
Misha Brukman44a562b2003-11-24 05:29:42 +0000297 if (!FullLibraryPath.empty() && IsSharedObject(FullLibraryPath))
Misha Brukman1df12622003-11-20 19:08:42 +0000298 Out2 << " -load=" << FullLibraryPath << " \\\n";
299 }
300 Out2 << " $0.bc $*\n";
John Criswell1997a342003-09-16 21:27:35 +0000301 Out2.close();
302 }
Chris Lattner5ff2e052002-01-24 19:12:12 +0000303
Chris Lattner1d496172003-04-19 22:44:38 +0000304 // Make the script executable...
Misha Brukman21663632003-09-30 17:59:25 +0000305 MakeFileExecutable(OutputFilename);
Misha Brukman90869942003-08-20 20:38:15 +0000306
John Criswella3ce8b42003-09-02 21:11:22 +0000307 // Make the bytecode file readable and directly executable in LLEE as well
Misha Brukman21663632003-09-30 17:59:25 +0000308 MakeFileExecutable(RealBytecodeOutput);
309 MakeFileReadable(RealBytecodeOutput);
Chris Lattner1d496172003-04-19 22:44:38 +0000310 }
Chris Lattner5ff2e052002-01-24 19:12:12 +0000311
312 return 0;
313}