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