Make sure to remove all dead type names from the symbol table, not just
[oota-llvm.git] / lib / Linker / LinkLibraries.cpp
1 //===- lib/Linker/LinkLibraries.cpp - Link LLVM libraries -----------------===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by Reid Spencer and is distributed under the 
6 // University of Illinois Open Source License. See LICENSE.TXT for details.
7 // 
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains routines to handle finding libraries and linking them in. 
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/Linker.h"
15 #include "llvm/Module.h"
16
17 using namespace llvm;
18
19 /// LinkInLibrary - links one library into the HeadModule
20 bool 
21 Linker::LinkInLibrary(const std::string& Lib) 
22 {
23   // Determine where this library lives.
24   sys::Path Pathname = FindLib(Lib);
25   if (Pathname.isEmpty())
26     return warning("Cannot find library '" + Lib + "'");
27
28   // If its an archive, try to link it in
29   if (Pathname.isArchive()) {
30     if (LinkInArchive(Pathname))
31       return error("Cannot link archive '" + Pathname.toString() + "'");
32   } else if (Pathname.isBytecodeFile()) {
33     // LLVM ".so" file.
34     if (LinkInFile(Pathname))
35       return error("Cannot link file '" + Pathname.toString() + "'");
36
37   } else if (Pathname.isDynamicLibrary()) {
38     return warning("Library '" + Lib + "' is a native dynamic library.");
39   } else {
40     return warning("Supposed library '" + Lib + "' isn't a library.");
41   }
42   return false;
43 }
44
45 /// LinkLibraries - takes the specified library files and links them into the
46 /// main bytecode object file.
47 ///
48 /// Inputs:
49 ///  Libraries  - The list of libraries to link into the module.
50 ///
51 /// Return value:
52 ///  FALSE - No error.
53 ///  TRUE  - Error.
54 ///
55 bool 
56 Linker::LinkInLibraries(const std::vector<std::string> &Libraries) {
57
58   // Process the set of libraries we've been provided
59   for (unsigned i = 0; i < Libraries.size(); ++i) {
60     if (LinkInLibrary(Libraries[i]))
61       return true;
62   }
63
64   // At this point we have processed all the libraries provided to us. Since
65   // we have an aggregated module at this point, the dependent libraries in
66   // that module should also be aggregated with duplicates eliminated. This is
67   // now the time to process the dependent libraries to resolve any remaining
68   // symbols.
69   const Module::LibraryListType& DepLibs = Composite->getLibraries();
70   for (Module::LibraryListType::const_iterator I = DepLibs.begin(), 
71       E = DepLibs.end(); I != E; ++I) {
72     if (LinkInLibrary(*I)) 
73       return true;
74   }
75   return false;
76 }