blob: 9a4c00680e44faf14451d2654532ca2b1dcbeb95 [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 Lattnerc8cc4cb2002-05-07 18:36:35 +000016#include "llvm/Transforms/Utils/Linker.h"
Chris Lattnere7fca512002-01-24 19:12:12 +000017#include "llvm/Module.h"
Chris Lattnerad202a02002-04-08 00:14:58 +000018#include "llvm/PassManager.h"
19#include "llvm/Bytecode/Reader.h"
20#include "llvm/Bytecode/WriteBytecodePass.h"
Chris Lattner9c3b55e2003-04-24 19:13:02 +000021#include "llvm/Target/TargetData.h"
Chris Lattnerd9d8c072002-07-23 22:04:43 +000022#include "llvm/Transforms/IPO.h"
Chris Lattnerd9d8c072002-07-23 22:04:43 +000023#include "llvm/Transforms/Scalar.h"
Chris Lattnere7fca512002-01-24 19:12:12 +000024#include "Support/CommandLine.h"
Chris Lattner76d12292002-04-18 19:55:25 +000025#include "Support/Signals.h"
Chris Lattnere7fca512002-01-24 19:12:12 +000026#include <fstream>
27#include <memory>
Chris Lattner10970eb2003-04-19 22:44:38 +000028#include <set>
Chris Lattner41c34652002-03-11 17:49:53 +000029#include <algorithm>
Chris Lattnere7fca512002-01-24 19:12:12 +000030#include <sys/types.h> // For FileExists
31#include <sys/stat.h>
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 Lattnera856db22003-04-18 23:38:22 +000059
Chris Lattner10970eb2003-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 Lattnera856db22003-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 Lattnerf3d4f172003-04-18 23:01:25 +000071}
Chris Lattnere7fca512002-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 Lattnere7fca512002-01-24 19:12:12 +000079
Chris Lattner10970eb2003-04-19 22:44:38 +000080// LoadObject - Read the specified "object file", which should not search the
81// library path to find it.
Chris Lattner7cb77e12003-05-13 22:14:13 +000082static inline std::auto_ptr<Module> LoadObject(std::string FN,
Chris Lattner10970eb2003-04-19 22:44:38 +000083 std::string &OutErrorMessage) {
84 if (Verbose) std::cerr << "Loading '" << FN << "'\n";
85 if (!FileExists(FN)) {
Chris Lattner7cb77e12003-05-13 22:14:13 +000086 // Attempt to load from the LLVM_LIB_SEARCH_PATH directory... if we would
87 // otherwise fail. This is used to locate objects like crtend.o.
88 //
89 char *SearchPath = getenv("LLVM_LIB_SEARCH_PATH");
90 if (SearchPath && FileExists(std::string(SearchPath)+"/"+FN))
91 FN = std::string(SearchPath)+"/"+FN;
92 else {
93 OutErrorMessage = "could not find input file '" + FN + "'!";
94 return std::auto_ptr<Module>();
95 }
Chris Lattnere7fca512002-01-24 19:12:12 +000096 }
97
Chris Lattner10970eb2003-04-19 22:44:38 +000098 std::string ErrorMessage;
99 Module *Result = ParseBytecodeFile(FN, &ErrorMessage);
100 if (Result) return std::auto_ptr<Module>(Result);
101
102 OutErrorMessage = "Bytecode file '" + FN + "' corrupt!";
103 if (ErrorMessage.size()) OutErrorMessage += ": " + ErrorMessage;
Chris Lattnere7fca512002-01-24 19:12:12 +0000104 return std::auto_ptr<Module>();
105}
106
107
Chris Lattner10970eb2003-04-19 22:44:38 +0000108static Module *LoadSingleLibraryObject(const std::string &Filename) {
109 std::string ErrorMessage;
110 std::auto_ptr<Module> M = LoadObject(Filename, ErrorMessage);
111 if (M.get() == 0 && Verbose) {
112 std::cerr << "Error loading '" + Filename + "'";
113 if (!ErrorMessage.empty()) std::cerr << ": " << ErrorMessage;
114 std::cerr << "\n";
115 }
116
117 return M.release();
118}
119
Brian Gaeke69a79602003-05-23 20:27:07 +0000120// IsArchive - Returns true iff FILENAME appears to be the name of an ar
121// archive file. It determines this by checking the magic string at the
122// beginning of the file.
123static bool IsArchive (const std::string &filename) {
124 static const std::string ArchiveMagic ("!<arch>\012");
125 char buf[1 + ArchiveMagic.size ()];
126 std::ifstream f (filename.c_str ());
127 f.read (buf, ArchiveMagic.size ());
128 buf[ArchiveMagic.size ()] = '\0';
129 return (ArchiveMagic == buf);
130}
Chris Lattner10970eb2003-04-19 22:44:38 +0000131
Brian Gaeke69a79602003-05-23 20:27:07 +0000132// LoadLibraryExactName - This looks for a file with a known name and tries to
133// load it, similarly to LoadLibraryFromDirectory().
134static inline bool LoadLibraryExactName (const std::string &FileName,
135 std::vector<Module*> &Objects, bool &isArchive) {
136 if (Verbose) std::cerr << " Considering '" << FileName << "'\n";
137 if (FileExists(FileName)) {
138 if (IsArchive (FileName)) {
139 std::string ErrorMessage;
140 if (Verbose) std::cerr << " Loading '" << FileName << "'\n";
141 if (!ReadArchiveFile(FileName, Objects, &ErrorMessage)) {
142 isArchive = true;
143 return false; // Success!
144 }
145 if (Verbose) {
146 std::cerr << " Error loading archive '" + FileName + "'";
147 if (!ErrorMessage.empty()) std::cerr << ": " << ErrorMessage;
148 std::cerr << "\n";
149 }
150 } else {
151 if (Module *M = LoadSingleLibraryObject(FileName)) {
152 isArchive = false;
153 Objects.push_back(M);
154 return false;
155 }
Chris Lattner10970eb2003-04-19 22:44:38 +0000156 }
157 }
Chris Lattner10970eb2003-04-19 22:44:38 +0000158 return true;
159}
160
Brian Gaeke69a79602003-05-23 20:27:07 +0000161// LoadLibrary - Try to load a library named LIBNAME that contains
162// LLVM bytecode. If SEARCH is true, then search for a file named
163// libLIBNAME.{a,so,bc} in the current library search path. Otherwise,
164// assume LIBNAME is the real name of the library file. This method puts
165// the loaded modules into the Objects list, and sets isArchive to true if
166// a .a file was loaded. It returns true if no library is found or if an
167// error occurs; otherwise it returns false.
Chris Lattner10970eb2003-04-19 22:44:38 +0000168//
169static inline bool LoadLibrary(const std::string &LibName,
170 std::vector<Module*> &Objects, bool &isArchive,
Brian Gaeke69a79602003-05-23 20:27:07 +0000171 bool search, std::string &ErrorMessage) {
172 if (search) {
173 // First, try the current directory. Then, iterate over the
174 // directories in LibPaths, looking for a suitable match for LibName
175 // in each one.
176 for (unsigned NextLibPathIdx = 0; NextLibPathIdx != LibPaths.size();
177 ++NextLibPathIdx) {
178 std::string Directory = LibPaths[NextLibPathIdx] + "/";
179 if (!LoadLibraryExactName(Directory + "lib" + LibName + ".a",
180 Objects, isArchive))
181 return false;
182 if (!LoadLibraryExactName(Directory + "lib" + LibName + ".so",
183 Objects, isArchive))
184 return false;
185 if (!LoadLibraryExactName(Directory + "lib" + LibName + ".bc",
186 Objects, isArchive))
187 return false;
188 }
189 } else {
190 // If they said no searching, then assume LibName is the real name.
191 if (!LoadLibraryExactName(LibName, Objects, isArchive))
Chris Lattner10970eb2003-04-19 22:44:38 +0000192 return false;
Chris Lattner10970eb2003-04-19 22:44:38 +0000193 }
Chris Lattner10970eb2003-04-19 22:44:38 +0000194 ErrorMessage = "error linking library '-l" + LibName+ "': library not found!";
195 return true;
196}
197
198static void GetAllDefinedSymbols(Module *M,
199 std::set<std::string> &DefinedSymbols) {
200 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
201 if (I->hasName() && !I->isExternal() && !I->hasInternalLinkage())
202 DefinedSymbols.insert(I->getName());
203 for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I)
204 if (I->hasName() && !I->isExternal() && !I->hasInternalLinkage())
205 DefinedSymbols.insert(I->getName());
206}
207
208// GetAllUndefinedSymbols - This calculates the set of undefined symbols that
209// still exist in an LLVM module. This is a bit tricky because there may be two
210// symbols with the same name, but different LLVM types that will be resolved to
211// each other, but aren't currently (thus we need to treat it as resolved).
212//
213static void GetAllUndefinedSymbols(Module *M,
214 std::set<std::string> &UndefinedSymbols) {
215 std::set<std::string> DefinedSymbols;
216 UndefinedSymbols.clear(); // Start out empty
217
218 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
219 if (I->hasName()) {
220 if (I->isExternal())
221 UndefinedSymbols.insert(I->getName());
222 else if (!I->hasInternalLinkage())
223 DefinedSymbols.insert(I->getName());
224 }
225 for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I)
226 if (I->hasName()) {
227 if (I->isExternal())
228 UndefinedSymbols.insert(I->getName());
229 else if (!I->hasInternalLinkage())
230 DefinedSymbols.insert(I->getName());
231 }
232
233 // Prune out any defined symbols from the undefined symbols set...
234 for (std::set<std::string>::iterator I = UndefinedSymbols.begin();
235 I != UndefinedSymbols.end(); )
236 if (DefinedSymbols.count(*I))
237 UndefinedSymbols.erase(I++); // This symbol really is defined!
238 else
239 ++I; // Keep this symbol in the undefined symbols list
240}
241
242
243static bool LinkLibrary(Module *M, const std::string &LibName,
Brian Gaeke69a79602003-05-23 20:27:07 +0000244 bool search, std::string &ErrorMessage) {
Chris Lattner7cb77e12003-05-13 22:14:13 +0000245 std::set<std::string> UndefinedSymbols;
246 GetAllUndefinedSymbols(M, UndefinedSymbols);
247 if (UndefinedSymbols.empty()) {
248 if (Verbose) std::cerr << " No symbols undefined, don't link library!\n";
249 return false; // No need to link anything in!
250 }
251
Chris Lattner10970eb2003-04-19 22:44:38 +0000252 std::vector<Module*> Objects;
253 bool isArchive;
Brian Gaeke69a79602003-05-23 20:27:07 +0000254 if (LoadLibrary(LibName, Objects, isArchive, search, ErrorMessage))
255 return true;
Chris Lattner10970eb2003-04-19 22:44:38 +0000256
257 // Figure out which symbols are defined by all of the modules in the .a file
258 std::vector<std::set<std::string> > DefinedSymbols;
259 DefinedSymbols.resize(Objects.size());
260 for (unsigned i = 0; i != Objects.size(); ++i)
261 GetAllDefinedSymbols(Objects[i], DefinedSymbols[i]);
262
Chris Lattner10970eb2003-04-19 22:44:38 +0000263 bool Linked = true;
264 while (Linked) { // While we are linking in object files, loop.
265 Linked = false;
266
267 for (unsigned i = 0; i != Objects.size(); ++i) {
268 // Consider whether we need to link in this module... we only need to
269 // link it in if it defines some symbol which is so far undefined.
270 //
271 const std::set<std::string> &DefSymbols = DefinedSymbols[i];
272
273 bool ObjectRequired = false;
274 for (std::set<std::string>::iterator I = UndefinedSymbols.begin(),
275 E = UndefinedSymbols.end(); I != E; ++I)
276 if (DefSymbols.count(*I)) {
277 if (Verbose)
278 std::cerr << " Found object providing symbol '" << *I << "'...\n";
279 ObjectRequired = true;
280 break;
281 }
282
283 // We DO need to link this object into the program...
284 if (ObjectRequired) {
285 if (LinkModules(M, Objects[i], &ErrorMessage))
286 return true; // Couldn't link in the right object file...
287
288 // Since we have linked in this object, delete it from the list of
289 // objects to consider in this archive file.
290 std::swap(Objects[i], Objects.back());
291 std::swap(DefinedSymbols[i], DefinedSymbols.back());
292 Objects.pop_back();
293 DefinedSymbols.pop_back();
294 --i; // Do not skip an entry
295
296 // The undefined symbols set should have shrunk.
297 GetAllUndefinedSymbols(M, UndefinedSymbols);
298 Linked = true; // We have linked something in!
299 }
300 }
301 }
302
303 return false;
304}
305
306static int PrintAndReturn(const char *progname, const std::string &Message,
307 const std::string &Extra = "") {
308 std::cerr << progname << Extra << ": " << Message << "\n";
309 return 1;
310}
311
312
Chris Lattnere7fca512002-01-24 19:12:12 +0000313int main(int argc, char **argv) {
Chris Lattner5ff62e92002-07-22 02:10:13 +0000314 cl::ParseCommandLineOptions(argc, argv, " llvm linker for GCC\n");
Chris Lattnere7fca512002-01-24 19:12:12 +0000315
Chris Lattnere7fca512002-01-24 19:12:12 +0000316 std::string ErrorMessage;
Chris Lattner10970eb2003-04-19 22:44:38 +0000317 std::auto_ptr<Module> Composite(LoadObject(InputFilenames[0], ErrorMessage));
318 if (Composite.get() == 0)
319 return PrintAndReturn(argv[0], ErrorMessage);
Chris Lattnere7fca512002-01-24 19:12:12 +0000320
Brian Gaeke69a79602003-05-23 20:27:07 +0000321 // We always look first in the current directory when searching for libraries.
322 LibPaths.insert(LibPaths.begin(), ".");
323
Chris Lattnerd34a51d2003-04-21 19:53:24 +0000324 // If the user specied an extra search path in their environment, respect it.
325 if (char *SearchPath = getenv("LLVM_LIB_SEARCH_PATH"))
326 LibPaths.push_back(SearchPath);
327
Chris Lattner10970eb2003-04-19 22:44:38 +0000328 for (unsigned i = 1; i < InputFilenames.size(); ++i) {
Brian Gaeke69a79602003-05-23 20:27:07 +0000329 // A user may specify an ar archive without -l, perhaps because it
330 // is not installed as a library. Detect that and link the library.
331 if (IsArchive (InputFilenames[i])) {
332 if (Verbose) std::cerr << "Linking archive '" << InputFilenames[i]
333 << "'\n";
334 if (LinkLibrary (Composite.get(), InputFilenames[i], false, ErrorMessage))
335 return PrintAndReturn(argv[0], ErrorMessage,
336 ": error linking in '" + InputFilenames[i] + "'");
337 continue;
338 }
339
Chris Lattner10970eb2003-04-19 22:44:38 +0000340 std::auto_ptr<Module> M(LoadObject(InputFilenames[i], ErrorMessage));
341 if (M.get() == 0)
342 return PrintAndReturn(argv[0], ErrorMessage);
Chris Lattnere7fca512002-01-24 19:12:12 +0000343
Chris Lattnerf3d4f172003-04-18 23:01:25 +0000344 if (Verbose) std::cerr << "Linking in '" << InputFilenames[i] << "'\n";
Chris Lattnere7fca512002-01-24 19:12:12 +0000345
Chris Lattner10970eb2003-04-19 22:44:38 +0000346 if (LinkModules(Composite.get(), M.get(), &ErrorMessage))
347 return PrintAndReturn(argv[0], ErrorMessage,
348 ": error linking in '" + InputFilenames[i] + "'");
349 }
350
Chris Lattnerc65b1042003-04-19 23:07:33 +0000351 // Remove any consecutive duplicates of the same library...
352 Libraries.erase(std::unique(Libraries.begin(), Libraries.end()),
353 Libraries.end());
354
Chris Lattner10970eb2003-04-19 22:44:38 +0000355 // Link in all of the libraries next...
356 for (unsigned i = 0; i != Libraries.size(); ++i) {
357 if (Verbose) std::cerr << "Linking in library: -l" << Libraries[i] << "\n";
Brian Gaeke69a79602003-05-23 20:27:07 +0000358 if (LinkLibrary(Composite.get(), Libraries[i], true, ErrorMessage))
Chris Lattner10970eb2003-04-19 22:44:38 +0000359 return PrintAndReturn(argv[0], ErrorMessage);
Chris Lattnere7fca512002-01-24 19:12:12 +0000360 }
361
Chris Lattnerf8b90ee2002-04-10 20:37:47 +0000362 // In addition to just linking the input from GCC, we also want to spiff it up
Chris Lattnerad202a02002-04-08 00:14:58 +0000363 // a little bit. Do this now.
364 //
365 PassManager Passes;
366
Chris Lattner9c3b55e2003-04-24 19:13:02 +0000367 // Add an appropriate TargetData instance for this module...
368 Passes.add(new TargetData("gccas", Composite.get()));
369
Chris Lattnerad202a02002-04-08 00:14:58 +0000370 // Linking modules together can lead to duplicated global constants, only keep
371 // one copy of each constant...
372 //
373 Passes.add(createConstantMergePass());
374
Chris Lattner2b598372002-04-08 05:18:12 +0000375 // If the -s command line option was specified, strip the symbols out of the
376 // resulting program to make it smaller. -s is a GCC option that we are
377 // supporting.
378 //
379 if (Strip)
380 Passes.add(createSymbolStrippingPass());
381
Chris Lattnerf8b90ee2002-04-10 20:37:47 +0000382 // Often if the programmer does not specify proper prototypes for the
383 // functions they are calling, they end up calling a vararg version of the
384 // function that does not get a body filled in (the real function has typed
385 // arguments). This pass merges the two functions.
386 //
387 Passes.add(createFunctionResolvingPass());
388
Chris Lattnerdabaa462003-04-16 21:43:22 +0000389 if (!NoInternalize) {
390 // Now that composite has been compiled, scan through the module, looking
391 // for a main function. If main is defined, mark all other functions
392 // internal.
393 //
394 Passes.add(createInternalizePass());
395 }
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000396
Chris Lattnerad202a02002-04-08 00:14:58 +0000397 // Now that we have optimized the program, discard unreachable functions...
398 //
399 Passes.add(createGlobalDCEPass());
Chris Lattner2fbfdcf2002-04-07 20:49:59 +0000400
Chris Lattnerad202a02002-04-08 00:14:58 +0000401 // Add the pass that writes bytecode to the output file...
Chris Lattner10970eb2003-04-19 22:44:38 +0000402 std::string RealBytecodeOutput = OutputFilename;
403 if (!LinkAsLibrary) RealBytecodeOutput += ".bc";
404 std::ofstream Out(RealBytecodeOutput.c_str());
405 if (!Out.good())
406 return PrintAndReturn(argv[0], "error opening '" + RealBytecodeOutput +
407 "' for writing!");
Chris Lattnerad202a02002-04-08 00:14:58 +0000408 Passes.add(new WriteBytecodePass(&Out)); // Write bytecode to file...
Chris Lattnere7fca512002-01-24 19:12:12 +0000409
Chris Lattner76d12292002-04-18 19:55:25 +0000410 // Make sure that the Out file gets unlink'd from the disk if we get a SIGINT
Chris Lattner10970eb2003-04-19 22:44:38 +0000411 RemoveFileOnSignal(RealBytecodeOutput);
Chris Lattner76d12292002-04-18 19:55:25 +0000412
Chris Lattnerad202a02002-04-08 00:14:58 +0000413 // Run our queue of passes all at once now, efficiently.
Chris Lattner0b12b5f2002-06-25 16:13:21 +0000414 Passes.run(*Composite.get());
Chris Lattnere7fca512002-01-24 19:12:12 +0000415 Out.close();
416
Chris Lattner10970eb2003-04-19 22:44:38 +0000417 if (!LinkAsLibrary) {
418 // Output the script to start the program...
419 std::ofstream Out2(OutputFilename.c_str());
420 if (!Out2.good())
421 return PrintAndReturn(argv[0], "error opening '" + OutputFilename +
422 "' for writing!");
423 Out2 << "#!/bin/sh\nlli -q -abort-on-exception $0.bc $*\n";
424 Out2.close();
Chris Lattnere7fca512002-01-24 19:12:12 +0000425
Chris Lattner10970eb2003-04-19 22:44:38 +0000426 // Make the script executable...
427 chmod(OutputFilename.c_str(), 0755);
428 }
Chris Lattnere7fca512002-01-24 19:12:12 +0000429
430 return 0;
431}