0d770f56f13e209e185e41fb47f9e45b8a902d5d
[oota-llvm.git] / lib / Linker / LinkArchives.cpp
1 //===- lib/Linker/LinkArchives.cpp - Link LLVM objects and libraries ------===//
2 // 
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 //===----------------------------------------------------------------------===//
9 //
10 // This file contains routines to handle linking together LLVM bytecode files,
11 // and to handle annoying things like static libraries.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "llvm/Linker.h"
16 #include "llvm/Module.h"
17 #include "llvm/ModuleProvider.h"
18 #include "llvm/PassManager.h"
19 #include "llvm/ADT/SetOperations.h"
20 #include "llvm/Bytecode/Reader.h"
21 #include "llvm/Bytecode/Archive.h"
22 #include "llvm/Bytecode/WriteBytecodePass.h"
23 #include "llvm/Target/TargetData.h"
24 #include "llvm/Transforms/IPO.h"
25 #include "llvm/Transforms/Scalar.h"
26 #include "llvm/Config/config.h"
27 #include "llvm/Support/CommandLine.h"
28 #include "llvm/Support/FileUtilities.h"
29 #include "llvm/Support/Timer.h"
30 #include "llvm/System/Signals.h"
31 #include "llvm/Support/SystemUtils.h"
32 #include <algorithm>
33 #include <fstream>
34 #include <memory>
35 #include <set>
36 using namespace llvm;
37
38 /// FindLib - Try to convert Filename into the name of a file that we can open,
39 /// if it does not already name a file we can open, by first trying to open
40 /// Filename, then libFilename.[suffix] for each of a set of several common
41 /// library suffixes, in each of the directories in Paths and the directory
42 /// named by the value of the environment variable LLVM_LIB_SEARCH_PATH. Returns
43 /// an empty string if no matching file can be found.
44 ///
45 std::string llvm::FindLib(const std::string &Filename,
46                           const std::vector<std::string> &Paths,
47                           bool SharedObjectOnly) {
48   // Determine if the pathname can be found as it stands.
49   if (FileOpenable(Filename))
50     return Filename;
51
52   // If that doesn't work, convert the name into a library name.
53   std::string LibName = "lib" + Filename;
54
55   // Iterate over the directories in Paths to see if we can find the library
56   // there.
57   for (unsigned Index = 0; Index != Paths.size(); ++Index) {
58     std::string Directory = Paths[Index] + "/";
59
60     if (!SharedObjectOnly && FileOpenable(Directory + LibName + ".bc"))
61       return Directory + LibName + ".bc";
62
63     if (FileOpenable(Directory + LibName + SHLIBEXT))
64       return Directory + LibName + SHLIBEXT;
65
66     if (!SharedObjectOnly && FileOpenable(Directory + LibName + ".a"))
67       return Directory + LibName + ".a";
68   }
69
70   // One last hope: Check LLVM_LIB_SEARCH_PATH.
71   char *SearchPath = getenv("LLVM_LIB_SEARCH_PATH");
72   if (SearchPath == NULL)
73     return std::string();
74
75   LibName = std::string(SearchPath) + "/" + LibName;
76   if (FileOpenable(LibName))
77     return LibName;
78
79   return std::string();
80 }
81
82 /// GetAllDefinedSymbols - Modifies its parameter DefinedSymbols to contain the
83 /// name of each externally-visible symbol defined in M.
84 ///
85 void llvm::GetAllDefinedSymbols(Module *M,
86                                 std::set<std::string> &DefinedSymbols) {
87   for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
88     if (I->hasName() && !I->isExternal() && !I->hasInternalLinkage())
89       DefinedSymbols.insert(I->getName());
90   for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I)
91     if (I->hasName() && !I->isExternal() && !I->hasInternalLinkage())
92       DefinedSymbols.insert(I->getName());
93 }
94
95 /// GetAllUndefinedSymbols - calculates the set of undefined symbols that still
96 /// exist in an LLVM module. This is a bit tricky because there may be two
97 /// symbols with the same name but different LLVM types that will be resolved to
98 /// each other but aren't currently (thus we need to treat it as resolved).
99 ///
100 /// Inputs:
101 ///  M - The module in which to find undefined symbols.
102 ///
103 /// Outputs:
104 ///  UndefinedSymbols - A set of C++ strings containing the name of all
105 ///                     undefined symbols.
106 ///
107 void
108 llvm::GetAllUndefinedSymbols(Module *M,
109                              std::set<std::string> &UndefinedSymbols) {
110   std::set<std::string> DefinedSymbols;
111   UndefinedSymbols.clear();
112   
113   for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
114     if (I->hasName()) {
115       if (I->isExternal())
116         UndefinedSymbols.insert(I->getName());
117       else if (!I->hasInternalLinkage())
118         DefinedSymbols.insert(I->getName());
119     }
120   for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I)
121     if (I->hasName()) {
122       if (I->isExternal())
123         UndefinedSymbols.insert(I->getName());
124       else if (!I->hasInternalLinkage())
125         DefinedSymbols.insert(I->getName());
126     }
127   
128   // Prune out any defined symbols from the undefined symbols set...
129   for (std::set<std::string>::iterator I = UndefinedSymbols.begin();
130        I != UndefinedSymbols.end(); )
131     if (DefinedSymbols.count(*I))
132       UndefinedSymbols.erase(I++);  // This symbol really is defined!
133     else
134       ++I; // Keep this symbol in the undefined symbols list
135 }
136
137
138 /// LoadObject - Read in and parse the bytecode file named by FN and return the
139 /// module it contains (wrapped in an auto_ptr), or 0 and set ErrorMessage if an
140 /// error occurs.
141 ///
142 static std::auto_ptr<Module> LoadObject(const std::string &FN,
143                                        std::string &ErrorMessage) {
144   std::string ParserErrorMessage;
145   Module *Result = ParseBytecodeFile(FN, &ParserErrorMessage);
146   if (Result) return std::auto_ptr<Module>(Result);
147   ErrorMessage = "Bytecode file '" + FN + "' could not be loaded";
148   if (ParserErrorMessage.size()) ErrorMessage += ": " + ParserErrorMessage;
149   return std::auto_ptr<Module>();
150 }
151
152 /// LinkInArchive - opens an archive library and link in all objects which
153 /// provide symbols that are currently undefined.
154 ///
155 /// Inputs:
156 ///  M        - The module in which to link the archives.
157 ///  Filename - The pathname of the archive.
158 ///  Verbose  - Flags whether verbose messages should be printed.
159 ///
160 /// Outputs:
161 ///  ErrorMessage - A C++ string detailing what error occurred, if any.
162 ///
163 /// Return Value:
164 ///  TRUE  - An error occurred.
165 ///  FALSE - No errors.
166 ///
167 bool llvm::LinkInArchive(Module *M,
168                          const std::string &Filename,
169                          std::string* ErrorMessage,
170                          bool Verbose)
171 {
172   // Find all of the symbols currently undefined in the bytecode program.
173   // If all the symbols are defined, the program is complete, and there is
174   // no reason to link in any archive files.
175   std::set<std::string> UndefinedSymbols;
176   GetAllUndefinedSymbols(M, UndefinedSymbols);
177   
178   if (UndefinedSymbols.empty()) {
179     if (Verbose) std::cerr << "  No symbols undefined, don't link library!\n";
180     return false;  // No need to link anything in!
181   }
182
183   // Open the archive file
184   if (Verbose) std::cerr << "  Loading archive file '" << Filename << "'\n";
185   std::auto_ptr<Archive> AutoArch (
186     Archive::OpenAndLoadSymbols(sys::Path(Filename)));
187
188   Archive* arch = AutoArch.get();
189
190   // Save a set of symbols that are not defined by the archive. Since we're
191   // entering a loop, there's no point searching for these multiple times. This
192   // variable is used to "set_subtract" from the set of undefined symbols.
193   std::set<std::string> NotDefinedByArchive;
194
195   // While we are linking in object files, loop.
196   while (true) {     
197
198     // Find the modules we need to link into the target module
199     std::set<ModuleProvider*> Modules;
200     arch->findModulesDefiningSymbols(UndefinedSymbols, Modules);
201
202     // If we didn't find any more modules to link this time, we are done 
203     // searching this archive.
204     if (Modules.empty())
205       break;
206
207     // Any symbols remaining in UndefinedSymbols after
208     // findModulesDefiningSymbols are ones that the archive does not define. So
209     // we add them to the NotDefinedByArchive variable now.
210     NotDefinedByArchive.insert(UndefinedSymbols.begin(),
211         UndefinedSymbols.end());
212
213     // Loop over all the ModuleProviders that we got back from the archive
214     for (std::set<ModuleProvider*>::iterator I=Modules.begin(), E=Modules.end();
215          I != E; ++I) {
216
217       // Get the module we must link in.
218       std::auto_ptr<Module> AutoModule( (*I)->releaseModule() );
219       Module* aModule = AutoModule.get();
220
221       // Link it in
222       if (LinkModules(M, aModule, ErrorMessage))
223         return true;   // Couldn't link in the module
224     }
225
226     // Get the undefined symbols from the aggregate module. This recomputes the
227     // symbols we still need after the new modules have been linked in.
228     GetAllUndefinedSymbols(M, UndefinedSymbols);
229
230     // At this point we have two sets of undefined symbols: UndefinedSymbols
231     // which holds the undefined symbols from all the modules, and 
232     // NotDefinedByArchive which holds symbols we know the archive doesn't
233     // define. There's no point searching for symbols that we won't find in the
234     // archive so we subtract these sets.
235     set_subtract<std::set<std::string>,std::set<std::string> >(
236         UndefinedSymbols,NotDefinedByArchive);
237     
238     // If there's no symbols left, no point in continuing to search the
239     // archive.
240     if (UndefinedSymbols.empty())
241       break;
242   }
243   
244   return false;
245 }
246
247 /// LinkInFile - opens a bytecode file and links in all objects which
248 /// provide symbols that are currently undefined.
249 ///
250 /// Inputs:
251 ///  HeadModule - The module in which to link the bytecode file.
252 ///  Filename   - The pathname of the bytecode file.
253 ///  Verbose    - Flags whether verbose messages should be printed.
254 ///
255 /// Outputs:
256 ///  ErrorMessage - A C++ string detailing what error occurred, if any.
257 ///
258 /// Return Value:
259 ///  TRUE  - An error occurred.
260 ///  FALSE - No errors.
261 ///
262 static bool LinkInFile(Module *HeadModule,
263                        const std::string &Filename,
264                        std::string &ErrorMessage,
265                        bool Verbose)
266 {
267   std::auto_ptr<Module> M(LoadObject(Filename, ErrorMessage));
268   if (M.get() == 0) return true;
269   bool Result = LinkModules(HeadModule, M.get(), &ErrorMessage);
270   if (Verbose) std::cerr << "Linked in bytecode file '" << Filename << "'\n";
271   return Result;
272 }
273
274 /// LinkFiles - takes a module and a list of files and links them all together.
275 /// It locates the file either in the current directory, as its absolute
276 /// or relative pathname, or as a file somewhere in LLVM_LIB_SEARCH_PATH.
277 ///
278 /// Inputs:
279 ///  progname   - The name of the program (infamous argv[0]).
280 ///  HeadModule - The module under which all files will be linked.
281 ///  Files      - A vector of C++ strings indicating the LLVM bytecode filenames
282 ///               to be linked.  The names can refer to a mixture of pure LLVM
283 ///               bytecode files and archive (ar) formatted files.
284 ///  Verbose    - Flags whether verbose output should be printed while linking.
285 ///
286 /// Outputs:
287 ///  HeadModule - The module will have the specified LLVM bytecode files linked
288 ///               in.
289 ///
290 /// Return value:
291 ///  FALSE - No errors.
292 ///  TRUE  - Some error occurred.
293 ///
294 bool llvm::LinkFiles(const char *progname, Module *HeadModule,
295                      const std::vector<std::string> &Files, bool Verbose) {
296   // String in which to receive error messages.
297   std::string ErrorMessage;
298
299   // Full pathname of the file
300   std::string Pathname;
301
302   // Get the library search path from the environment
303   char *SearchPath = getenv("LLVM_LIB_SEARCH_PATH");
304
305   for (unsigned i = 0; i < Files.size(); ++i) {
306     // Determine where this file lives.
307     if (FileOpenable(Files[i])) {
308       Pathname = Files[i];
309     } else {
310       if (SearchPath == NULL) {
311         std::cerr << progname << ": Cannot find linker input file '"
312                   << Files[i] << "'\n";
313         std::cerr << progname
314                   << ": Warning: Your LLVM_LIB_SEARCH_PATH is unset.\n";
315         return true;
316       }
317
318       Pathname = std::string(SearchPath)+"/"+Files[i];
319       if (!FileOpenable(Pathname)) {
320         std::cerr << progname << ": Cannot find linker input file '"
321                   << Files[i] << "'\n";
322         return true;
323       }
324     }
325
326     // A user may specify an ar archive without -l, perhaps because it
327     // is not installed as a library. Detect that and link the library.
328     if (IsArchive(Pathname)) {
329       if (Verbose)
330         std::cerr << "Trying to link archive '" << Pathname << "'\n";
331
332       if (LinkInArchive(HeadModule, Pathname, &ErrorMessage, Verbose)) {
333         std::cerr << progname << ": Error linking in archive '" << Pathname 
334                   << "': " << ErrorMessage << "\n";
335         return true;
336       }
337     } else if (IsBytecode(Pathname)) {
338       if (Verbose)
339         std::cerr << "Trying to link bytecode file '" << Pathname << "'\n";
340
341       if (LinkInFile(HeadModule, Pathname, ErrorMessage, Verbose)) {
342         std::cerr << progname << ": Error linking in bytecode file '"
343                   << Pathname << "': " << ErrorMessage << "\n";
344         return true;
345       }
346     } else {
347       std::cerr << progname << ": Warning: invalid file `" << Pathname 
348                 << "' ignored.\n";
349     }
350   }
351
352   return false;
353 }
354
355 /// LinkLibraries - takes the specified library files and links them into the
356 /// main bytecode object file.
357 ///
358 /// Inputs:
359 ///  progname   - The name of the program (infamous argv[0]).
360 ///  HeadModule - The module into which all necessary libraries will be linked.
361 ///  Libraries  - The list of libraries to link into the module.
362 ///  LibPaths   - The list of library paths in which to find libraries.
363 ///  Verbose    - Flags whether verbose messages should be printed.
364 ///  Native     - Flags whether native code is being generated.
365 ///
366 /// Outputs:
367 ///  HeadModule - The module will have all necessary libraries linked in.
368 ///
369 /// Return value:
370 ///  FALSE - No error.
371 ///  TRUE  - Error.
372 ///
373 void llvm::LinkLibraries(const char *progname, Module *HeadModule,
374                          const std::vector<std::string> &Libraries,
375                          const std::vector<std::string> &LibPaths,
376                          bool Verbose, bool Native) {
377   // String in which to receive error messages.
378   std::string ErrorMessage;
379
380   for (unsigned i = 0; i < Libraries.size(); ++i) {
381     // Determine where this library lives.
382     std::string Pathname = FindLib(Libraries[i], LibPaths);
383     if (Pathname.empty()) {
384       // If the pathname does not exist, then continue to the next one if
385       // we're doing a native link and give an error if we're doing a bytecode
386       // link.
387       if (!Native) {
388         std::cerr << progname << ": WARNING: Cannot find library -l"
389                   << Libraries[i] << "\n";
390         continue;
391       }
392     }
393
394     // A user may specify an ar archive without -l, perhaps because it
395     // is not installed as a library. Detect that and link the library.
396     if (IsArchive(Pathname)) {
397       if (Verbose)
398         std::cerr << "Trying to link archive '" << Pathname << "' (-l"
399                   << Libraries[i] << ")\n";
400
401       if (LinkInArchive(HeadModule, Pathname, &ErrorMessage, Verbose)) {
402         std::cerr << progname << ": " << ErrorMessage
403                   << ": Error linking in archive '" << Pathname << "' (-l"
404                   << Libraries[i] << ")\n";
405         exit(1);
406       }
407     } else if (IsBytecode(Pathname)) {
408       if (Verbose)
409         std::cerr << "Trying to link bytecode file '" << Pathname
410                   << "' (-l" << Libraries[i] << ")\n";
411
412       if (LinkInFile(HeadModule, Pathname, ErrorMessage, Verbose)) {
413         std::cerr << progname << ": " << ErrorMessage
414                   << ": error linking in bytecode file '" << Pathname << "' (-l"
415                   << Libraries[i] << ")\n";
416         exit(1);
417       }
418     }
419   }
420 }