blob: 24c624ece2c93b72cda4a423771fa76ac752de93 [file] [log] [blame]
Chris Lattner1d496172003-04-19 22:44:38 +00001//===- gccld.cpp - LLVM 'ld' compatible linker ----------------------------===//
Chris Lattner5ff2e052002-01-24 19:12:12 +00002//
3// This utility is intended to be compatible with GCC, and follows standard
Chris Lattner1d496172003-04-19 22:44:38 +00004// system 'ld' conventions. As such, the default output file is ./a.out.
Chris Lattner5ff2e052002-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,
12// I'm not to worried about this.
13//
14//===----------------------------------------------------------------------===//
15
Chris Lattner7608a462002-05-07 18:36:35 +000016#include "llvm/Transforms/Utils/Linker.h"
Chris Lattner5ff2e052002-01-24 19:12:12 +000017#include "llvm/Module.h"
Chris Lattner3b08c2f2002-04-08 00:14:58 +000018#include "llvm/PassManager.h"
19#include "llvm/Bytecode/Reader.h"
20#include "llvm/Bytecode/WriteBytecodePass.h"
Chris Lattnerd571e2a2003-04-24 19:13:02 +000021#include "llvm/Target/TargetData.h"
Chris Lattner35c45412002-07-23 22:04:43 +000022#include "llvm/Transforms/IPO.h"
Chris Lattner35c45412002-07-23 22:04:43 +000023#include "llvm/Transforms/Scalar.h"
Chris Lattner5ff2e052002-01-24 19:12:12 +000024#include "Support/CommandLine.h"
Chris Lattnerc065ad82002-04-18 19:55:25 +000025#include "Support/Signals.h"
Chris Lattner5ff2e052002-01-24 19:12:12 +000026#include <fstream>
27#include <memory>
Chris Lattner1d496172003-04-19 22:44:38 +000028#include <set>
Chris Lattner6f0d4532002-03-11 17:49:53 +000029#include <algorithm>
Chris Lattner5ff2e052002-01-24 19:12:12 +000030#include <sys/types.h> // For FileExists
31#include <sys/stat.h>
Chris Lattner5ff2e052002-01-24 19:12:12 +000032
Chris Lattner2b3a5db2003-04-18 23:01:25 +000033namespace {
34 cl::list<std::string>
35 InputFilenames(cl::Positional, cl::desc("<input bytecode files>"),
36 cl::OneOrMore);
Chris Lattnerf5cad152002-07-22 02:10:13 +000037
Chris Lattner2b3a5db2003-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 Lattnerf5cad152002-07-22 02:10:13 +000041
Chris Lattner2b3a5db2003-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 Lattnerf5cad152002-07-22 02:10:13 +000048
Chris Lattner2b3a5db2003-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 Lattnerf5cad152002-07-22 02:10:13 +000052
Chris Lattner2b3a5db2003-04-18 23:01:25 +000053 cl::opt<bool>
54 Strip("s", cl::desc("Strip symbol info from executable"));
Chris Lattnerf5cad152002-07-22 02:10:13 +000055
Chris Lattner2b3a5db2003-04-18 23:01:25 +000056 cl::opt<bool>
57 NoInternalize("disable-internalize",
58 cl::desc("Do not mark all symbols as internal"));
Chris Lattner602d2092003-04-18 23:38:22 +000059
Chris Lattner1d496172003-04-19 22:44:38 +000060 cl::opt<bool>
61 LinkAsLibrary("link-as-library", cl::desc("Link the .bc files together as a"
62 " library, not an executable"));
63
Chris Lattner602d2092003-04-18 23:38:22 +000064 // Compatibility options that are ignored, but support by LD
65 cl::opt<std::string>
66 CO3("soname", cl::Hidden, cl::desc("Compatibility option: ignored"));
67 cl::opt<std::string>
68 CO4("version-script", cl::Hidden, cl::desc("Compatibility option: ignored"));
69 cl::opt<bool>
70 CO5("eh-frame-hdr", cl::Hidden, cl::desc("Compatibility option: ignored"));
Chris Lattner2b3a5db2003-04-18 23:01:25 +000071}
Chris Lattner5ff2e052002-01-24 19:12:12 +000072
73// FileExists - Return true if the specified string is an openable file...
74static inline bool FileExists(const std::string &FN) {
75 struct stat StatBuf;
76 return stat(FN.c_str(), &StatBuf) != -1;
77}
78
Chris Lattner5ff2e052002-01-24 19:12:12 +000079
Chris Lattner1d496172003-04-19 22:44:38 +000080// LoadObject - Read the specified "object file", which should not search the
81// library path to find it.
82static inline std::auto_ptr<Module> LoadObject(const std::string &FN,
83 std::string &OutErrorMessage) {
84 if (Verbose) std::cerr << "Loading '" << FN << "'\n";
85 if (!FileExists(FN)) {
86 OutErrorMessage = "could not find input file '" + FN + "'!";
87 return std::auto_ptr<Module>();
Chris Lattner5ff2e052002-01-24 19:12:12 +000088 }
89
Chris Lattner1d496172003-04-19 22:44:38 +000090 std::string ErrorMessage;
91 Module *Result = ParseBytecodeFile(FN, &ErrorMessage);
92 if (Result) return std::auto_ptr<Module>(Result);
93
94 OutErrorMessage = "Bytecode file '" + FN + "' corrupt!";
95 if (ErrorMessage.size()) OutErrorMessage += ": " + ErrorMessage;
Chris Lattner5ff2e052002-01-24 19:12:12 +000096 return std::auto_ptr<Module>();
97}
98
99
Chris Lattner1d496172003-04-19 22:44:38 +0000100static Module *LoadSingleLibraryObject(const std::string &Filename) {
101 std::string ErrorMessage;
102 std::auto_ptr<Module> M = LoadObject(Filename, ErrorMessage);
103 if (M.get() == 0 && Verbose) {
104 std::cerr << "Error loading '" + Filename + "'";
105 if (!ErrorMessage.empty()) std::cerr << ": " << ErrorMessage;
106 std::cerr << "\n";
107 }
108
109 return M.release();
110}
111
112
113// LoadLibraryFromDirectory - This looks for a .a, .so, or .bc file in a
114// particular directory. It returns true if no library is found, otherwise it
115// puts the loaded modules into the Objects list, and sets isArchive to true if
116// a .a file was loaded.
117//
118static inline bool LoadLibraryFromDirectory(const std::string &LibName,
119 const std::string &Directory,
120 std::vector<Module*> &Objects,
121 bool &isArchive) {
122 if (FileExists(Directory + "lib" + LibName + ".a")) {
123 std::string ErrorMessage;
Chris Lattner4b462c02003-04-19 23:07:33 +0000124 if (Verbose) std::cerr << " Loading '" << Directory << "lib"
125 << LibName << ".a'\n";
Chris Lattner1d496172003-04-19 22:44:38 +0000126 if (!ReadArchiveFile(Directory + "lib" + LibName + ".a", Objects,
127 &ErrorMessage)) { // Read the archive file
128 isArchive = true;
129 return false; // Success!
130 }
131
132 if (Verbose) {
Chris Lattner4b462c02003-04-19 23:07:33 +0000133 std::cerr << " Error loading archive '" + Directory +"lib"+LibName+".a'";
Chris Lattner1d496172003-04-19 22:44:38 +0000134 if (!ErrorMessage.empty()) std::cerr << ": " << ErrorMessage;
135 std::cerr << "\n";
136 }
137 }
138
139 if (FileExists(Directory + "lib" + LibName + ".so"))
140 if (Module *M = LoadSingleLibraryObject(Directory + "lib" + LibName+".so")){
141 isArchive = false;
142 Objects.push_back(M);
143 return false;
144 }
145
146 if (FileExists(Directory + "lib" + LibName + ".bc"))
147 if (Module *M = LoadSingleLibraryObject(Directory + "lib" + LibName+".bc")){
148 isArchive = false;
149 Objects.push_back(M);
150 return false;
151 }
152 return true;
153}
154
155// LoadLibrary - This searches for a .a, .so, or .bc file which provides the
156// LLVM bytecode for the library. It returns true if no library is found,
157// otherwise it puts the loaded modules into the Objects list, and sets
158// isArchive to true if a .a file was loaded.
159//
160static inline bool LoadLibrary(const std::string &LibName,
161 std::vector<Module*> &Objects, bool &isArchive,
162 std::string &ErrorMessage) {
163 std::string Directory;
164 unsigned NextLibPathIdx = 0;
165
166 while (1) {
167 // Try loading from the current directory...
168 if (Verbose) std::cerr << " Looking in directory '" << Directory << "'\n";
169 if (!LoadLibraryFromDirectory(LibName, Directory, Objects, isArchive))
170 return false;
171
172 if (NextLibPathIdx == LibPaths.size()) break;
173 Directory = LibPaths[NextLibPathIdx++]+"/";
174 }
175
176 ErrorMessage = "error linking library '-l" + LibName+ "': library not found!";
177 return true;
178}
179
180static void GetAllDefinedSymbols(Module *M,
181 std::set<std::string> &DefinedSymbols) {
182 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
183 if (I->hasName() && !I->isExternal() && !I->hasInternalLinkage())
184 DefinedSymbols.insert(I->getName());
185 for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I)
186 if (I->hasName() && !I->isExternal() && !I->hasInternalLinkage())
187 DefinedSymbols.insert(I->getName());
188}
189
190// GetAllUndefinedSymbols - This calculates the set of undefined symbols that
191// still exist in an LLVM module. This is a bit tricky because there may be two
192// symbols with the same name, but different LLVM types that will be resolved to
193// each other, but aren't currently (thus we need to treat it as resolved).
194//
195static void GetAllUndefinedSymbols(Module *M,
196 std::set<std::string> &UndefinedSymbols) {
197 std::set<std::string> DefinedSymbols;
198 UndefinedSymbols.clear(); // Start out empty
199
200 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
201 if (I->hasName()) {
202 if (I->isExternal())
203 UndefinedSymbols.insert(I->getName());
204 else if (!I->hasInternalLinkage())
205 DefinedSymbols.insert(I->getName());
206 }
207 for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I)
208 if (I->hasName()) {
209 if (I->isExternal())
210 UndefinedSymbols.insert(I->getName());
211 else if (!I->hasInternalLinkage())
212 DefinedSymbols.insert(I->getName());
213 }
214
215 // Prune out any defined symbols from the undefined symbols set...
216 for (std::set<std::string>::iterator I = UndefinedSymbols.begin();
217 I != UndefinedSymbols.end(); )
218 if (DefinedSymbols.count(*I))
219 UndefinedSymbols.erase(I++); // This symbol really is defined!
220 else
221 ++I; // Keep this symbol in the undefined symbols list
222}
223
224
225static bool LinkLibrary(Module *M, const std::string &LibName,
226 std::string &ErrorMessage) {
227 std::vector<Module*> Objects;
228 bool isArchive;
229 if (LoadLibrary(LibName, Objects, isArchive, ErrorMessage)) return true;
230
231 // Figure out which symbols are defined by all of the modules in the .a file
232 std::vector<std::set<std::string> > DefinedSymbols;
233 DefinedSymbols.resize(Objects.size());
234 for (unsigned i = 0; i != Objects.size(); ++i)
235 GetAllDefinedSymbols(Objects[i], DefinedSymbols[i]);
236
237 std::set<std::string> UndefinedSymbols;
238 GetAllUndefinedSymbols(M, UndefinedSymbols);
239
240 bool Linked = true;
241 while (Linked) { // While we are linking in object files, loop.
242 Linked = false;
243
244 for (unsigned i = 0; i != Objects.size(); ++i) {
245 // Consider whether we need to link in this module... we only need to
246 // link it in if it defines some symbol which is so far undefined.
247 //
248 const std::set<std::string> &DefSymbols = DefinedSymbols[i];
249
250 bool ObjectRequired = false;
251 for (std::set<std::string>::iterator I = UndefinedSymbols.begin(),
252 E = UndefinedSymbols.end(); I != E; ++I)
253 if (DefSymbols.count(*I)) {
254 if (Verbose)
255 std::cerr << " Found object providing symbol '" << *I << "'...\n";
256 ObjectRequired = true;
257 break;
258 }
259
260 // We DO need to link this object into the program...
261 if (ObjectRequired) {
262 if (LinkModules(M, Objects[i], &ErrorMessage))
263 return true; // Couldn't link in the right object file...
264
265 // Since we have linked in this object, delete it from the list of
266 // objects to consider in this archive file.
267 std::swap(Objects[i], Objects.back());
268 std::swap(DefinedSymbols[i], DefinedSymbols.back());
269 Objects.pop_back();
270 DefinedSymbols.pop_back();
271 --i; // Do not skip an entry
272
273 // The undefined symbols set should have shrunk.
274 GetAllUndefinedSymbols(M, UndefinedSymbols);
275 Linked = true; // We have linked something in!
276 }
277 }
278 }
279
280 return false;
281}
282
283static int PrintAndReturn(const char *progname, const std::string &Message,
284 const std::string &Extra = "") {
285 std::cerr << progname << Extra << ": " << Message << "\n";
286 return 1;
287}
288
289
Chris Lattner5ff2e052002-01-24 19:12:12 +0000290int main(int argc, char **argv) {
Chris Lattnerf5cad152002-07-22 02:10:13 +0000291 cl::ParseCommandLineOptions(argc, argv, " llvm linker for GCC\n");
Chris Lattner5ff2e052002-01-24 19:12:12 +0000292
Chris Lattner5ff2e052002-01-24 19:12:12 +0000293 std::string ErrorMessage;
Chris Lattner1d496172003-04-19 22:44:38 +0000294 std::auto_ptr<Module> Composite(LoadObject(InputFilenames[0], ErrorMessage));
295 if (Composite.get() == 0)
296 return PrintAndReturn(argv[0], ErrorMessage);
Chris Lattner5ff2e052002-01-24 19:12:12 +0000297
Chris Lattnerda3bc212003-04-21 19:53:24 +0000298 // If the user specied an extra search path in their environment, respect it.
299 if (char *SearchPath = getenv("LLVM_LIB_SEARCH_PATH"))
300 LibPaths.push_back(SearchPath);
301
Chris Lattner1d496172003-04-19 22:44:38 +0000302 for (unsigned i = 1; i < InputFilenames.size(); ++i) {
303 std::auto_ptr<Module> M(LoadObject(InputFilenames[i], ErrorMessage));
304 if (M.get() == 0)
305 return PrintAndReturn(argv[0], ErrorMessage);
Chris Lattner5ff2e052002-01-24 19:12:12 +0000306
Chris Lattner2b3a5db2003-04-18 23:01:25 +0000307 if (Verbose) std::cerr << "Linking in '" << InputFilenames[i] << "'\n";
Chris Lattner5ff2e052002-01-24 19:12:12 +0000308
Chris Lattner1d496172003-04-19 22:44:38 +0000309 if (LinkModules(Composite.get(), M.get(), &ErrorMessage))
310 return PrintAndReturn(argv[0], ErrorMessage,
311 ": error linking in '" + InputFilenames[i] + "'");
312 }
313
Chris Lattner4b462c02003-04-19 23:07:33 +0000314 // Remove any consecutive duplicates of the same library...
315 Libraries.erase(std::unique(Libraries.begin(), Libraries.end()),
316 Libraries.end());
317
Chris Lattner1d496172003-04-19 22:44:38 +0000318 // Link in all of the libraries next...
319 for (unsigned i = 0; i != Libraries.size(); ++i) {
320 if (Verbose) std::cerr << "Linking in library: -l" << Libraries[i] << "\n";
321 if (LinkLibrary(Composite.get(), Libraries[i], ErrorMessage))
322 return PrintAndReturn(argv[0], ErrorMessage);
Chris Lattner5ff2e052002-01-24 19:12:12 +0000323 }
324
Chris Lattnerc34061f2002-04-10 20:37:47 +0000325 // In addition to just linking the input from GCC, we also want to spiff it up
Chris Lattner3b08c2f2002-04-08 00:14:58 +0000326 // a little bit. Do this now.
327 //
328 PassManager Passes;
329
Chris Lattnerd571e2a2003-04-24 19:13:02 +0000330 // Add an appropriate TargetData instance for this module...
331 Passes.add(new TargetData("gccas", Composite.get()));
332
Chris Lattner3b08c2f2002-04-08 00:14:58 +0000333 // Linking modules together can lead to duplicated global constants, only keep
334 // one copy of each constant...
335 //
336 Passes.add(createConstantMergePass());
337
Chris Lattner2b33d752002-04-08 05:18:12 +0000338 // If the -s command line option was specified, strip the symbols out of the
339 // resulting program to make it smaller. -s is a GCC option that we are
340 // supporting.
341 //
342 if (Strip)
343 Passes.add(createSymbolStrippingPass());
344
Chris Lattnerc34061f2002-04-10 20:37:47 +0000345 // Often if the programmer does not specify proper prototypes for the
346 // functions they are calling, they end up calling a vararg version of the
347 // function that does not get a body filled in (the real function has typed
348 // arguments). This pass merges the two functions.
349 //
350 Passes.add(createFunctionResolvingPass());
351
Chris Lattnera9a98802003-04-16 21:43:22 +0000352 if (!NoInternalize) {
353 // Now that composite has been compiled, scan through the module, looking
354 // for a main function. If main is defined, mark all other functions
355 // internal.
356 //
357 Passes.add(createInternalizePass());
358 }
Chris Lattner62b7fd12002-04-07 20:49:59 +0000359
Chris Lattner3b08c2f2002-04-08 00:14:58 +0000360 // Now that we have optimized the program, discard unreachable functions...
361 //
362 Passes.add(createGlobalDCEPass());
Chris Lattner62b7fd12002-04-07 20:49:59 +0000363
Chris Lattner3b08c2f2002-04-08 00:14:58 +0000364 // Add the pass that writes bytecode to the output file...
Chris Lattner1d496172003-04-19 22:44:38 +0000365 std::string RealBytecodeOutput = OutputFilename;
366 if (!LinkAsLibrary) RealBytecodeOutput += ".bc";
367 std::ofstream Out(RealBytecodeOutput.c_str());
368 if (!Out.good())
369 return PrintAndReturn(argv[0], "error opening '" + RealBytecodeOutput +
370 "' for writing!");
Chris Lattner3b08c2f2002-04-08 00:14:58 +0000371 Passes.add(new WriteBytecodePass(&Out)); // Write bytecode to file...
Chris Lattner5ff2e052002-01-24 19:12:12 +0000372
Chris Lattnerc065ad82002-04-18 19:55:25 +0000373 // Make sure that the Out file gets unlink'd from the disk if we get a SIGINT
Chris Lattner1d496172003-04-19 22:44:38 +0000374 RemoveFileOnSignal(RealBytecodeOutput);
Chris Lattnerc065ad82002-04-18 19:55:25 +0000375
Chris Lattner3b08c2f2002-04-08 00:14:58 +0000376 // Run our queue of passes all at once now, efficiently.
Chris Lattner7076ff22002-06-25 16:13:21 +0000377 Passes.run(*Composite.get());
Chris Lattner5ff2e052002-01-24 19:12:12 +0000378 Out.close();
379
Chris Lattner1d496172003-04-19 22:44:38 +0000380 if (!LinkAsLibrary) {
381 // Output the script to start the program...
382 std::ofstream Out2(OutputFilename.c_str());
383 if (!Out2.good())
384 return PrintAndReturn(argv[0], "error opening '" + OutputFilename +
385 "' for writing!");
386 Out2 << "#!/bin/sh\nlli -q -abort-on-exception $0.bc $*\n";
387 Out2.close();
Chris Lattner5ff2e052002-01-24 19:12:12 +0000388
Chris Lattner1d496172003-04-19 22:44:38 +0000389 // Make the script executable...
390 chmod(OutputFilename.c_str(), 0755);
391 }
Chris Lattner5ff2e052002-01-24 19:12:12 +0000392
393 return 0;
394}