LLVM API Documentation

Linker.cpp

Go to the documentation of this file.
00001 //===- lib/Linker/Linker.cpp - Basic Linker functionality  ----------------===//
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 basic Linker functionality that all usages will need.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "llvm/Linker.h"
00015 #include "llvm/Module.h"
00016 #include "llvm/Bitcode/ReaderWriter.h"
00017 #include "llvm/Config/config.h"
00018 #include "llvm/Support/MemoryBuffer.h"
00019 #include "llvm/Support/raw_ostream.h"
00020 using namespace llvm;
00021 
00022 Linker::Linker(const StringRef &progname, const StringRef &modname,
00023                LLVMContext& C, unsigned flags): 
00024   Context(C),
00025   Composite(new Module(modname, C)),
00026   LibPaths(),
00027   Flags(flags),
00028   Error(),
00029   ProgramName(progname) { }
00030 
00031 Linker::Linker(const StringRef &progname, Module* aModule, unsigned flags) : 
00032   Context(aModule->getContext()),
00033   Composite(aModule),
00034   LibPaths(),
00035   Flags(flags),
00036   Error(),
00037   ProgramName(progname) { }
00038 
00039 Linker::~Linker() {
00040   delete Composite;
00041 }
00042 
00043 bool
00044 Linker::error(const StringRef &message) {
00045   Error = message;
00046   if (!(Flags&QuietErrors))
00047     errs() << ProgramName << ": error: " << message << "\n";
00048   return true;
00049 }
00050 
00051 bool
00052 Linker::warning(const StringRef &message) {
00053   Error = message;
00054   if (!(Flags&QuietWarnings))
00055     errs() << ProgramName << ": warning: " << message << "\n";
00056   return false;
00057 }
00058 
00059 void
00060 Linker::verbose(const StringRef &message) {
00061   if (Flags&Verbose)
00062     errs() << "  " << message << "\n";
00063 }
00064 
00065 void
00066 Linker::addPath(const sys::Path& path) {
00067   LibPaths.push_back(path);
00068 }
00069 
00070 void
00071 Linker::addPaths(const std::vector<std::string>& paths) {
00072   for (unsigned i = 0; i != paths.size(); ++i) {
00073     sys::Path aPath;
00074     aPath.set(paths[i]);
00075     LibPaths.push_back(aPath);
00076   }
00077 }
00078 
00079 void
00080 Linker::addSystemPaths() {
00081   sys::Path::GetBitcodeLibraryPaths(LibPaths);
00082   LibPaths.insert(LibPaths.begin(),sys::Path("./"));
00083 }
00084 
00085 Module*
00086 Linker::releaseModule() {
00087   Module* result = Composite;
00088   LibPaths.clear();
00089   Error.clear();
00090   Composite = 0;
00091   Flags = 0;
00092   return result;
00093 }
00094 
00095 // LoadObject - Read in and parse the bitcode file named by FN and return the
00096 // module it contains (wrapped in an auto_ptr), or auto_ptr<Module>() and set
00097 // Error if an error occurs.
00098 std::auto_ptr<Module>
00099 Linker::LoadObject(const sys::Path &FN) {
00100   std::string ParseErrorMessage;
00101   Module *Result = 0;
00102   
00103   const std::string &FNS = FN.toString();
00104   std::auto_ptr<MemoryBuffer> Buffer(MemoryBuffer::getFileOrSTDIN(FNS.c_str()));
00105   if (Buffer.get())
00106     Result = ParseBitcodeFile(Buffer.get(), Context, &ParseErrorMessage);
00107   else
00108     ParseErrorMessage = "Error reading file '" + FNS + "'";
00109     
00110   if (Result)
00111     return std::auto_ptr<Module>(Result);
00112   Error = "Bitcode file '" + FN.toString() + "' could not be loaded";
00113   if (ParseErrorMessage.size())
00114     Error += ": " + ParseErrorMessage;
00115   return std::auto_ptr<Module>();
00116 }
00117 
00118 // IsLibrary - Determine if "Name" is a library in "Directory". Return
00119 // a non-empty sys::Path if its found, an empty one otherwise.
00120 static inline sys::Path IsLibrary(const StringRef &Name,
00121                                   const sys::Path &Directory) {
00122 
00123   sys::Path FullPath(Directory);
00124 
00125   // Try the libX.a form
00126   FullPath.appendComponent(("lib" + Name).str());
00127   FullPath.appendSuffix("a");
00128   if (FullPath.isArchive())
00129     return FullPath;
00130 
00131   // Try the libX.bca form
00132   FullPath.eraseSuffix();
00133   FullPath.appendSuffix("bca");
00134   if (FullPath.isArchive())
00135     return FullPath;
00136 
00137   // Try the libX.so (or .dylib) form
00138   FullPath.eraseSuffix();
00139   FullPath.appendSuffix(&(LTDL_SHLIB_EXT[1]));
00140   if (FullPath.isDynamicLibrary())  // Native shared library?
00141     return FullPath;
00142   if (FullPath.isBitcodeFile())    // .so file containing bitcode?
00143     return FullPath;
00144 
00145   // Not found .. fall through
00146 
00147   // Indicate that the library was not found in the directory.
00148   FullPath.clear();
00149   return FullPath;
00150 }
00151 
00152 /// FindLib - Try to convert Filename into the name of a file that we can open,
00153 /// if it does not already name a file we can open, by first trying to open
00154 /// Filename, then libFilename.[suffix] for each of a set of several common
00155 /// library suffixes, in each of the directories in LibPaths. Returns an empty
00156 /// Path if no matching file can be found.
00157 ///
00158 sys::Path
00159 Linker::FindLib(const StringRef &Filename) {
00160   // Determine if the pathname can be found as it stands.
00161   sys::Path FilePath(Filename);
00162   if (FilePath.canRead() &&
00163       (FilePath.isArchive() || FilePath.isDynamicLibrary()))
00164     return FilePath;
00165 
00166   // Iterate over the directories in Paths to see if we can find the library
00167   // there.
00168   for (unsigned Index = 0; Index != LibPaths.size(); ++Index) {
00169     sys::Path Directory(LibPaths[Index]);
00170     sys::Path FullPath = IsLibrary(Filename, Directory);
00171     if (!FullPath.isEmpty())
00172       return FullPath;
00173   }
00174   return sys::Path();
00175 }