1 //===- lib/Linker/Linker.cpp - Basic Linker functionality ----------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains basic Linker functionality that all usages will need.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Linker.h"
15 #include "llvm/Module.h"
16 #include "llvm/Bitcode/ReaderWriter.h"
17 #include "llvm/Support/Path.h"
18 #include "llvm/Support/MemoryBuffer.h"
19 #include "llvm/Support/raw_ostream.h"
20 #include "llvm/Config/config.h"
21 #include "llvm/Support/system_error.h"
24 Linker::Linker(StringRef progname, StringRef modname,
25 LLVMContext& C, unsigned flags):
27 Composite(new Module(modname, C)),
31 ProgramName(progname) { }
33 Linker::Linker(StringRef progname, Module* aModule, unsigned flags) :
34 Context(aModule->getContext()),
39 ProgramName(progname) { }
46 Linker::error(StringRef message) {
48 if (!(Flags&QuietErrors))
49 errs() << ProgramName << ": error: " << message << "\n";
54 Linker::warning(StringRef message) {
56 if (!(Flags&QuietWarnings))
57 errs() << ProgramName << ": warning: " << message << "\n";
62 Linker::verbose(StringRef message) {
64 errs() << " " << message << "\n";
68 Linker::addPath(const sys::Path& path) {
69 LibPaths.push_back(path);
73 Linker::addPaths(const std::vector<std::string>& paths) {
74 for (unsigned i = 0, e = paths.size(); i != e; ++i)
75 LibPaths.push_back(sys::Path(paths[i]));
79 Linker::addSystemPaths() {
80 sys::Path::GetBitcodeLibraryPaths(LibPaths);
81 LibPaths.insert(LibPaths.begin(),sys::Path("./"));
85 Linker::releaseModule() {
86 Module* result = Composite;
94 // LoadObject - Read in and parse the bitcode file named by FN and return the
95 // module it contains (wrapped in an auto_ptr), or auto_ptr<Module>() and set
96 // Error if an error occurs.
98 Linker::LoadObject(const sys::Path &FN) {
99 std::string ParseErrorMessage;
103 std::auto_ptr<MemoryBuffer> Buffer(
104 MemoryBuffer::getFileOrSTDIN(FN.c_str(), ec));
106 Result = ParseBitcodeFile(Buffer.get(), Context, &ParseErrorMessage);
108 ParseErrorMessage = "Error reading file '" + FN.str() + "'" + ": "
112 return std::auto_ptr<Module>(Result);
113 Error = "Bitcode file '" + FN.str() + "' could not be loaded";
114 if (ParseErrorMessage.size())
115 Error += ": " + ParseErrorMessage;
116 return std::auto_ptr<Module>();
119 // IsLibrary - Determine if "Name" is a library in "Directory". Return
120 // a non-empty sys::Path if its found, an empty one otherwise.
121 static inline sys::Path IsLibrary(StringRef Name,
122 const sys::Path &Directory) {
124 sys::Path FullPath(Directory);
126 // Try the libX.a form
127 FullPath.appendComponent(("lib" + Name).str());
128 FullPath.appendSuffix("a");
129 if (FullPath.isArchive())
132 // Try the libX.bca form
133 FullPath.eraseSuffix();
134 FullPath.appendSuffix("bca");
135 if (FullPath.isArchive())
138 // Try the libX.so (or .dylib) form
139 FullPath.eraseSuffix();
140 FullPath.appendSuffix(sys::Path::GetDLLSuffix());
141 if (FullPath.isDynamicLibrary()) // Native shared library?
143 if (FullPath.isBitcodeFile()) // .so file containing bitcode?
146 // Not found .. fall through
148 // Indicate that the library was not found in the directory.
153 /// FindLib - Try to convert Filename into the name of a file that we can open,
154 /// if it does not already name a file we can open, by first trying to open
155 /// Filename, then libFilename.[suffix] for each of a set of several common
156 /// library suffixes, in each of the directories in LibPaths. Returns an empty
157 /// Path if no matching file can be found.
160 Linker::FindLib(StringRef Filename) {
161 // Determine if the pathname can be found as it stands.
162 sys::Path FilePath(Filename);
163 if (FilePath.canRead() &&
164 (FilePath.isArchive() || FilePath.isDynamicLibrary()))
167 // Iterate over the directories in Paths to see if we can find the library
169 for (unsigned Index = 0; Index != LibPaths.size(); ++Index) {
170 sys::Path Directory(LibPaths[Index]);
171 sys::Path FullPath = IsLibrary(Filename, Directory);
172 if (!FullPath.isEmpty())