4c4fd6edaef227a34b3b8f2ff3ca8e24596b401e
[oota-llvm.git] / tools / gccld / Linker.cpp
1 //===- Linker.cpp - Link together LLVM objects and libraries --------------===//
2 //
3 // This file contains routines to handle linking together LLVM bytecode files,
4 // and to handle annoying things like static libraries.
5 //
6 //===----------------------------------------------------------------------===//
7
8 #include "gccld.h"
9 #include "llvm/Module.h"
10 #include "llvm/PassManager.h"
11 #include "llvm/Bytecode/Reader.h"
12 #include "llvm/Bytecode/WriteBytecodePass.h"
13 #include "llvm/Target/TargetData.h"
14 #include "llvm/Transforms/IPO.h"
15 #include "llvm/Transforms/Scalar.h"
16 #include "llvm/Transforms/Utils/Linker.h"
17 #include "Support/CommandLine.h"
18 #include "Support/FileUtilities.h"
19 #include "Support/Signals.h"
20 #include "Support/SystemUtils.h"
21 #include "Config/stdlib.h"
22 #include <algorithm>
23 #include <fstream>
24 #include <memory>
25 #include <set>
26
27 //
28 // Function: FileExists ()
29 //
30 // Description:
31 //  Determine if the specified filename exists and is readable.
32 //
33 // Inputs:
34 //  FN - The name of the file.
35 //
36 // Outputs:
37 //  None.
38 //
39 // Return Value:
40 //  TRUE - The file exists and is readable.
41 //  FALSE - The file does not exist or is unreadable.
42 //
43 static inline bool FileExists(const std::string &FN) {
44   return access(FN.c_str(), R_OK | F_OK) != -1;
45 }
46
47 //
48 // Function: IsArchive ()
49 //
50 // Description:
51 //  Determine if the specified file is an ar archive.  It determines this by
52 //  checking the magic string at the beginning of the file.
53 //
54 // Inputs:
55 //  filename - A C++ string containing the name of the file.
56 //
57 // Outputs:
58 //  None.
59 //
60 // Return value:
61 //  TRUE  - The file is an archive.
62 //  FALSE - The file is not an archive.
63 //
64 static inline bool IsArchive(const std::string &filename) {
65   std::string ArchiveMagic("!<arch>\012");
66   char buf[1 + ArchiveMagic.size()];
67   std::ifstream f(filename.c_str());
68   f.read(buf, ArchiveMagic.size());
69   buf[ArchiveMagic.size()] = '\0';
70   return ArchiveMagic == buf;
71 }
72
73 //
74 // Function: FindLib ()
75 //
76 // Description:
77 //  This function locates a particular library.  It will prepend and append
78 //  various directories, prefixes, and suffixes until it can find the library.
79 //
80 // Inputs:
81 //  Filename  - Name of the file to find.
82 //  Paths     - List of directories to search.
83 //
84 // Outputs:
85 //  None.
86 //
87 // Return value:
88 //  The name of the file is returned.
89 //  If the file is not found, an empty string is returned.
90 //
91 static std::string
92 FindLib(const std::string &Filename, const std::vector<std::string> &Paths) {
93   // Determine if the pathname can be found as it stands.
94   if (FileExists(Filename))
95     return Filename;
96
97   // If that doesn't work, convert the name into a library name.
98   std::string LibName = "lib" + Filename;
99
100   // Iterate over the directories in Paths to see if we can find the library
101   // there.
102   for (unsigned Index = 0; Index != Paths.size(); ++Index) {
103     std::string Directory = Paths[Index] + "/";
104
105     if (FileExists(Directory + LibName + ".bc"))
106       return Directory + LibName + ".bc";
107
108     if (FileExists(Directory + LibName + ".so"))
109       return Directory + LibName + ".so";
110
111     if (FileExists(Directory + LibName + ".a"))
112       return Directory + LibName + ".a";
113   }
114
115   // One last hope: Check LLVM_LIB_SEARCH_PATH.
116   char *SearchPath = getenv("LLVM_LIB_SEARCH_PATH");
117   if (SearchPath == NULL)
118     return std::string();
119
120   LibName = std::string(SearchPath) + "/" + LibName;
121   if (FileExists(LibName))
122     return LibName;
123
124   return std::string();
125 }
126
127 //
128 // Function: GetAllDefinedSymbols ()
129 //
130 // Description:
131 //  Find all of the defined symbols in the specified module.
132 //
133 // Inputs:
134 //  M - The module in which to find defined symbols.
135 //
136 // Outputs:
137 //  DefinedSymbols - A set of C++ strings that will contain the name of all
138 //                   defined symbols.
139 //
140 // Return value:
141 //  None.
142 //
143 void GetAllDefinedSymbols(Module *M, std::set<std::string> &DefinedSymbols) {
144   for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
145     if (I->hasName() && !I->isExternal() && !I->hasInternalLinkage())
146       DefinedSymbols.insert(I->getName());
147   for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I)
148     if (I->hasName() && !I->isExternal() && !I->hasInternalLinkage())
149       DefinedSymbols.insert(I->getName());
150 }
151
152 //
153 // Function: GetAllUndefinedSymbols ()
154 //
155 // Description:
156 //  This calculates the set of undefined symbols that still exist in an LLVM
157 //  module.  This is a bit tricky because there may be two symbols with the
158 //  same name but different LLVM types that will be resolved to each other but
159 //  aren't currently (thus we need to treat it as resolved).
160 //
161 // Inputs:
162 //  M - The module in which to find undefined symbols.
163 //
164 // Outputs:
165 //  UndefinedSymbols - A set of C++ strings containing the name of all
166 //                     undefined symbols.
167 //
168 // Return value:
169 //  None.
170 //
171 void
172 GetAllUndefinedSymbols(Module *M, std::set<std::string> &UndefinedSymbols) {
173   std::set<std::string> DefinedSymbols;
174   UndefinedSymbols.clear();   // Start out empty
175   
176   for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
177     if (I->hasName()) {
178       if (I->isExternal())
179         UndefinedSymbols.insert(I->getName());
180       else if (!I->hasInternalLinkage())
181         DefinedSymbols.insert(I->getName());
182     }
183   for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I)
184     if (I->hasName()) {
185       if (I->isExternal())
186         UndefinedSymbols.insert(I->getName());
187       else if (!I->hasInternalLinkage())
188         DefinedSymbols.insert(I->getName());
189     }
190   
191   // Prune out any defined symbols from the undefined symbols set...
192   for (std::set<std::string>::iterator I = UndefinedSymbols.begin();
193        I != UndefinedSymbols.end(); )
194     if (DefinedSymbols.count(*I))
195       UndefinedSymbols.erase(I++);  // This symbol really is defined!
196     else
197       ++I; // Keep this symbol in the undefined symbols list
198 }
199
200
201 //
202 // Function: LoadObject ()
203 //
204 // Description:
205 //  Read the specified bytecode object file.
206 //
207 // Inputs:
208 //  FN - The name of the file to load.
209 //
210 // Outputs:
211 //  OutErrorMessage - The error message to give back to the caller.
212 //
213 // Return Value:
214 //  A pointer to a module represening the bytecode file is returned.
215 //  If an error occurs, the pointer is 0.
216 //
217 std::auto_ptr<Module>
218 LoadObject(const std::string & FN, std::string &OutErrorMessage) {
219   std::string ErrorMessage;
220   Module *Result = ParseBytecodeFile(FN, &ErrorMessage);
221   if (Result) return std::auto_ptr<Module>(Result);
222   OutErrorMessage = "Bytecode file '" + FN + "' corrupt!";
223   if (ErrorMessage.size()) OutErrorMessage += ": " + ErrorMessage;
224   return std::auto_ptr<Module>();
225 }
226
227 //
228 // Function: LinkInArchive ()
229 //
230 // Description:
231 //  This function will open an archive library and link in all objects which
232 //  provide symbols that are currently undefined.
233 //
234 // Inputs:
235 //  M        - The module in which to link the archives.
236 //  Filename - The pathname of the archive.
237 //  Verbose  - Flags whether verbose messages should be printed.
238 //
239 // Outputs:
240 //  ErrorMessage - A C++ string detailing what error occurred, if any.
241 //
242 // Return Value:
243 //  TRUE  - An error occurred.
244 //  FALSE - No errors.
245 //
246 static bool LinkInArchive(Module *M,
247                           const std::string &Filename,
248                           std::string &ErrorMessage,
249                           bool Verbose)
250 {
251   // Find all of the symbols currently undefined in the bytecode program.
252   // If all the symbols are defined, the program is complete, and there is
253   // no reason to link in any archive files.
254   std::set<std::string> UndefinedSymbols;
255   GetAllUndefinedSymbols(M, UndefinedSymbols);
256   if (UndefinedSymbols.empty()) {
257     if (Verbose) std::cerr << "  No symbols undefined, don't link library!\n";
258     return false;  // No need to link anything in!
259   }
260
261   // Load in the archive objects.
262   if (Verbose) std::cerr << "  Loading '" << Filename << "'\n";
263   std::vector<Module*> Objects;
264   if (ReadArchiveFile(Filename, Objects, &ErrorMessage))
265     return true;
266
267   // Figure out which symbols are defined by all of the modules in the archive.
268   std::vector<std::set<std::string> > DefinedSymbols;
269   DefinedSymbols.resize(Objects.size());
270   for (unsigned i = 0; i != Objects.size(); ++i) {
271     GetAllDefinedSymbols(Objects[i], DefinedSymbols[i]);
272   }
273
274   // While we are linking in object files, loop.
275   bool Linked = true;
276   while (Linked) {     
277     Linked = false;
278
279     for (unsigned i = 0; i != Objects.size(); ++i) {
280       // Consider whether we need to link in this module...  we only need to
281       // link it in if it defines some symbol which is so far undefined.
282       //
283       const std::set<std::string> &DefSymbols = DefinedSymbols[i];
284
285       bool ObjectRequired = false;
286       for (std::set<std::string>::iterator I = UndefinedSymbols.begin(),
287              E = UndefinedSymbols.end(); I != E; ++I)
288         if (DefSymbols.count(*I)) {
289           if (Verbose)
290             std::cerr << "  Found object providing symbol '" << *I << "'...\n";
291           ObjectRequired = true;
292           break;
293         }
294       
295       // We DO need to link this object into the program...
296       if (ObjectRequired) {
297         if (LinkModules(M, Objects[i], &ErrorMessage))
298           return true;   // Couldn't link in the right object file...        
299         
300         // Since we have linked in this object, delete it from the list of
301         // objects to consider in this archive file.
302         std::swap(Objects[i], Objects.back());
303         std::swap(DefinedSymbols[i], DefinedSymbols.back());
304         Objects.pop_back();
305         DefinedSymbols.pop_back();
306         --i;   // Do not skip an entry
307         
308         // The undefined symbols set should have shrunk.
309         GetAllUndefinedSymbols(M, UndefinedSymbols);
310         Linked = true;  // We have linked something in!
311       }
312     }
313   }
314   
315   return false;
316 }
317
318 //
319 // Function: LinkInFile ()
320 //
321 // Description:
322 //  This function will open an archive library and link in all objects which
323 //  provide symbols that are currently undefined.
324 //
325 // Inputs:
326 //  HeadModule - The module in which to link the archives.
327 //  Filename   - The pathname of the archive.
328 //  Verbose    - Flags whether verbose messages should be printed.
329 //
330 // Outputs:
331 //  ErrorMessage - A C++ string detailing what error occurred, if any.
332 //
333 // Return Value:
334 //  TRUE  - An error occurred.
335 //  FALSE - No errors.
336 //
337 static bool LinkInFile(Module *HeadModule,
338                        const std::string &Filename,
339                        std::string &ErrorMessage,
340                        bool Verbose)
341 {
342   std::auto_ptr<Module> M(LoadObject(Filename, ErrorMessage));
343   if (M.get() == 0) return true;
344   if (Verbose) std::cerr << "Linking in '" << Filename << "'\n";
345   return LinkModules(HeadModule, M.get(), &ErrorMessage);
346 }
347
348 //
349 // Function: LinkFiles ()
350 //
351 // Description:
352 //  This function takes a module and a list of files and links them all
353 //  together.  It locates the file either in the current directory, as it's
354 //  absolute or relative pathname, or as a file somewhere in
355 //  LLVM_LIB_SEARCH_PATH.
356 //
357 // Inputs:
358 //  progname   - The name of the program (infamous argv[0]).
359 //  HeadModule - The module under which all files will be linked.
360 //  Files      - A vector of C++ strings indicating the LLVM bytecode filenames
361 //               to be linked.  The names can refer to a mixture of pure LLVM
362 //               bytecode files and archive (ar) formatted files.
363 //  Verbose    - Flags whether verbose output should be printed while linking.
364 //
365 // Outputs:
366 //  HeadModule - The module will have the specified LLVM bytecode files linked
367 //               in.
368 //
369 // Return value:
370 //  FALSE - No errors.
371 //  TRUE  - Some error occurred.
372 //
373 bool LinkFiles(const char *progname,
374                Module *HeadModule,
375                const std::vector<std::string> &Files,
376                bool Verbose)
377 {
378   // String in which to receive error messages.
379   std::string ErrorMessage;
380
381   // Full pathname of the file
382   std::string Pathname;
383
384   // Get the library search path from the environment
385   char *SearchPath = getenv("LLVM_LIB_SEARCH_PATH");
386
387   for (unsigned i = 1; i < Files.size(); ++i) {
388     // Determine where this file lives.
389     if (FileExists(Files[i])) {
390       Pathname = Files[i];
391     } else {
392       if (SearchPath == NULL) {
393         std::cerr << "Cannot find linker input file '" << Files[i] << "'";
394         return true;
395       }
396
397       Pathname = std::string(SearchPath)+"/"+Files[i];
398       if (!FileExists(Pathname)) {
399         std::cerr << "Cannot find linker input file '" << Files[i] << "'";
400         return true;
401       }
402     }
403
404     // A user may specify an ar archive without -l, perhaps because it
405     // is not installed as a library. Detect that and link the library.
406     if (IsArchive(Pathname)) {
407       if (Verbose)
408         std::cerr << "Linking archive '" << Files[i] << "'\n";
409
410       if (LinkInArchive(HeadModule, Pathname, ErrorMessage, Verbose)) {
411         PrintAndReturn(progname, ErrorMessage,
412                        ": Error linking in '" + Files[i] + "'");
413         return true;
414       }
415     } else {
416       if (Verbose)
417         std::cerr << "Linking file '" << Files[i] << "'\n";
418
419       if (LinkInFile(HeadModule, Pathname, ErrorMessage, Verbose)) {
420         PrintAndReturn(progname, ErrorMessage,
421                        ": error linking in '" + Files[i] + "'");
422         return true;
423       }
424     }
425   }
426
427   return false;
428 }
429
430 //
431 // Function: LinkLibraries ()
432 //
433 // Description:
434 //  This function takes the specified library files and links them into the
435 //  main bytecode object file.
436 //
437 // Inputs:
438 //  progname   - The name of the program (infamous argv[0]).
439 //  HeadModule - The module into which all necessary libraries will be linked.
440 //  Libraries  - The list of libraries to link into the module.
441 //  LibPaths   - The list of library paths in which to find libraries.
442 //  Verbose    - Flags whether verbose messages should be printed.
443 //  Native     - Flags whether native code is being generated.
444 //
445 // Outputs:
446 //  HeadModule - The module will have all necessary libraries linked in.
447 //
448 // Return value:
449 //  FALSE - No error.
450 //  TRUE  - Error.
451 //
452 bool LinkLibraries(const char *progname,
453                    Module *HeadModule,
454                    const std::vector<std::string> &Libraries,
455                    const std::vector<std::string> &LibPaths,
456                    bool Verbose,
457                    bool Native)
458 {
459   // String in which to receive error messages.
460   std::string ErrorMessage;
461
462   for (unsigned i = 1; i < Libraries.size(); ++i) {
463     // Determine where this library lives.
464     std::string Pathname = FindLib(Libraries[i], LibPaths);
465     if (Pathname.empty()) {
466       // If the pathname does not exist, then continue to the next one if
467       // we're doing a native link and give an error if we're doing a bytecode
468       // link.
469       if (!Native) {
470         PrintAndReturn(progname, "Cannot find " + Libraries[i]);
471         return true;
472       }
473     }
474
475     // A user may specify an ar archive without -l, perhaps because it
476     // is not installed as a library. Detect that and link the library.
477     if (IsArchive(Pathname)) {
478       if (Verbose)
479         std::cerr << "Linking archive '" << Libraries[i] << "'\n";
480
481       if (LinkInArchive(HeadModule, Pathname, ErrorMessage, Verbose)) {
482         PrintAndReturn(progname, ErrorMessage,
483                        ": Error linking in '" + Libraries[i] + "'");
484         return true;
485       }
486     } else {
487       if (Verbose)
488         std::cerr << "Linking file '" << Libraries[i] << "'\n";
489
490       if (LinkInFile(HeadModule, Pathname, ErrorMessage, Verbose)) {
491         PrintAndReturn(progname, ErrorMessage,
492                        ": error linking in '" + Libraries[i] + "'");
493         return true;
494       }
495     }
496   }
497
498   return false;
499 }