LLVM API Documentation

LinkItems.cpp

Go to the documentation of this file.
00001 //===- lib/Linker/LinkItems.cpp - Link LLVM objects and libraries ---------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file contains routines to handle linking together LLVM bitcode files,
00011 // and to handle annoying things like static libraries.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #include "llvm/Linker.h"
00016 #include "llvm/Module.h"
00017 #include "llvm/Support/ErrorHandling.h"
00018 #include "llvm/Support/MemoryBuffer.h"
00019 #include "llvm/Bitcode/ReaderWriter.h"
00020 
00021 using namespace llvm;
00022 
00023 // LinkItems - This function is the main entry point into linking. It takes a
00024 // list of LinkItem which indicates the order the files should be linked and
00025 // how each file should be treated (plain file or with library search). The
00026 // function only links bitcode and produces a result list of items that are
00027 // native objects. 
00028 bool
00029 Linker::LinkInItems(const ItemList& Items, ItemList& NativeItems) {
00030   // Clear the NativeItems just in case
00031   NativeItems.clear();
00032 
00033   // For each linkage item ...
00034   for (ItemList::const_iterator I = Items.begin(), E = Items.end();
00035        I != E; ++I) {
00036     if (I->second) {
00037       // Link in the library suggested.
00038       bool is_native = false;
00039       if (LinkInLibrary(I->first, is_native))
00040         return true;
00041       if (is_native)
00042         NativeItems.push_back(*I);
00043     } else {
00044       // Link in the file suggested
00045       bool is_native = false;
00046       if (LinkInFile(sys::Path(I->first), is_native))
00047         return true;
00048       if (is_native)
00049         NativeItems.push_back(*I);
00050     }
00051   }
00052 
00053   // At this point we have processed all the link items provided to us. Since
00054   // we have an aggregated module at this point, the dependent libraries in
00055   // that module should also be aggregated with duplicates eliminated. This is
00056   // now the time to process the dependent libraries to resolve any remaining
00057   // symbols.
00058   bool is_native;
00059   for (Module::lib_iterator I = Composite->lib_begin(),
00060          E = Composite->lib_end(); I != E; ++I) {
00061     if(LinkInLibrary(*I, is_native))
00062       return true;
00063     if (is_native)
00064       NativeItems.push_back(std::make_pair(*I, true));
00065   }
00066 
00067   return false;
00068 }
00069 
00070 
00071 /// LinkInLibrary - links one library into the HeadModule.
00072 ///
00073 bool Linker::LinkInLibrary(const StringRef &Lib, bool& is_native) {
00074   is_native = false;
00075   // Determine where this library lives.
00076   sys::Path Pathname = FindLib(Lib);
00077   if (Pathname.isEmpty())
00078     return error("Cannot find library '" + Lib.str() + "'");
00079 
00080   // If its an archive, try to link it in
00081   std::string Magic;
00082   Pathname.getMagicNumber(Magic, 64);
00083   switch (sys::IdentifyFileType(Magic.c_str(), 64)) {
00084     default: llvm_unreachable("Bad file type identification");
00085     case sys::Unknown_FileType:
00086       return warning("Supposed library '" + Lib.str() + "' isn't a library.");
00087 
00088     case sys::Bitcode_FileType:
00089       // LLVM ".so" file.
00090       if (LinkInFile(Pathname, is_native))
00091         return true;
00092       break;
00093 
00094     case sys::Archive_FileType:
00095       if (LinkInArchive(Pathname, is_native))
00096         return error("Cannot link archive '" + Pathname.toString() + "'");
00097       break;
00098 
00099     case sys::ELF_Relocatable_FileType:
00100     case sys::ELF_SharedObject_FileType:
00101     case sys::Mach_O_Object_FileType:
00102     case sys::Mach_O_FixedVirtualMemorySharedLib_FileType:
00103     case sys::Mach_O_DynamicallyLinkedSharedLib_FileType:
00104     case sys::Mach_O_DynamicallyLinkedSharedLibStub_FileType:
00105     case sys::COFF_FileType:
00106       is_native = true;
00107       break;
00108   }
00109   return false;
00110 }
00111 
00112 /// LinkLibraries - takes the specified library files and links them into the
00113 /// main bitcode object file.
00114 ///
00115 /// Inputs:
00116 ///  Libraries  - The list of libraries to link into the module.
00117 ///
00118 /// Return value:
00119 ///  FALSE - No error.
00120 ///  TRUE  - Error.
00121 ///
00122 bool Linker::LinkInLibraries(const std::vector<std::string> &Libraries) {
00123 
00124   // Process the set of libraries we've been provided.
00125   bool is_native = false;
00126   for (unsigned i = 0; i < Libraries.size(); ++i)
00127     if (LinkInLibrary(Libraries[i], is_native))
00128       return true;
00129 
00130   // At this point we have processed all the libraries provided to us. Since
00131   // we have an aggregated module at this point, the dependent libraries in
00132   // that module should also be aggregated with duplicates eliminated. This is
00133   // now the time to process the dependent libraries to resolve any remaining
00134   // symbols.
00135   const Module::LibraryListType& DepLibs = Composite->getLibraries();
00136   for (Module::LibraryListType::const_iterator I = DepLibs.begin(),
00137          E = DepLibs.end(); I != E; ++I)
00138     if (LinkInLibrary(*I, is_native))
00139       return true;
00140 
00141   return false;
00142 }
00143 
00144 /// LinkInFile - opens a bitcode file and links in all objects which
00145 /// provide symbols that are currently undefined.
00146 ///
00147 /// Inputs:
00148 ///  File - The pathname of the bitcode file.
00149 ///
00150 /// Outputs:
00151 ///  ErrorMessage - A C++ string detailing what error occurred, if any.
00152 ///
00153 /// Return Value:
00154 ///  TRUE  - An error occurred.
00155 ///  FALSE - No errors.
00156 ///
00157 bool Linker::LinkInFile(const sys::Path &File, bool &is_native) {
00158   is_native = false;
00159   
00160   // Check for a file of name "-", which means "read standard input"
00161   if (File.toString() == "-") {
00162     std::auto_ptr<Module> M;
00163     if (MemoryBuffer *Buffer = MemoryBuffer::getSTDIN()) {
00164       M.reset(ParseBitcodeFile(Buffer, Context, &Error));
00165       delete Buffer;
00166       if (M.get())
00167         if (!LinkInModule(M.get(), &Error))
00168           return false;
00169     } else 
00170       Error = "standard input is empty";
00171     return error("Cannot link stdin: " + Error);
00172   }
00173 
00174   // Make sure we can at least read the file
00175   if (!File.canRead())
00176     return error("Cannot find linker input '" + File.toString() + "'");
00177 
00178   // If its an archive, try to link it in
00179   std::string Magic;
00180   File.getMagicNumber(Magic, 64);
00181   switch (sys::IdentifyFileType(Magic.c_str(), 64)) {
00182     default: llvm_unreachable("Bad file type identification");
00183     case sys::Unknown_FileType:
00184       return warning("Ignoring file '" + File.toString() + 
00185                    "' because does not contain bitcode.");
00186 
00187     case sys::Archive_FileType:
00188       // A user may specify an ar archive without -l, perhaps because it
00189       // is not installed as a library. Detect that and link the archive.
00190       verbose("Linking archive file '" + File.toString() + "'");
00191       if (LinkInArchive(File, is_native))
00192         return true;
00193       break;
00194 
00195     case sys::Bitcode_FileType: {
00196       verbose("Linking bitcode file '" + File.toString() + "'");
00197       std::auto_ptr<Module> M(LoadObject(File));
00198       if (M.get() == 0)
00199         return error("Cannot load file '" + File.toString() + "': " + Error);
00200       if (LinkInModule(M.get(), &Error))
00201         return error("Cannot link file '" + File.toString() + "': " + Error);
00202 
00203       verbose("Linked in file '" + File.toString() + "'");
00204       break;
00205     }
00206 
00207     case sys::ELF_Relocatable_FileType:
00208     case sys::ELF_SharedObject_FileType:
00209     case sys::Mach_O_Object_FileType:
00210     case sys::Mach_O_FixedVirtualMemorySharedLib_FileType:
00211     case sys::Mach_O_DynamicallyLinkedSharedLib_FileType:
00212     case sys::Mach_O_DynamicallyLinkedSharedLibStub_FileType:
00213     case sys::COFF_FileType:
00214       is_native = true;
00215       break;
00216   }
00217   return false;
00218 }
00219 
00220 /// LinkFiles - takes a module and a list of files and links them all together.
00221 /// It locates the file either in the current directory, as its absolute
00222 /// or relative pathname, or as a file somewhere in LLVM_LIB_SEARCH_PATH.
00223 ///
00224 /// Inputs:
00225 ///  Files      - A vector of sys::Path indicating the LLVM bitcode filenames
00226 ///               to be linked.  The names can refer to a mixture of pure LLVM
00227 ///               bitcode files and archive (ar) formatted files.
00228 ///
00229 /// Return value:
00230 ///  FALSE - No errors.
00231 ///  TRUE  - Some error occurred.
00232 ///
00233 bool Linker::LinkInFiles(const std::vector<sys::Path> &Files) {
00234   bool is_native;
00235   for (unsigned i = 0; i < Files.size(); ++i)
00236     if (LinkInFile(Files[i], is_native))
00237       return true;
00238   return false;
00239 }