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