blob: 59fbceb6a3080f93a8d4851c96aa45031bec02f3 [file] [log] [blame]
Reid Spencer903f21d2004-12-13 03:50:50 +00001//===- lib/Linker/Linker.cpp - Basic Linker functionality ----------------===//
Misha Brukmanf976c852005-04-21 22:55:34 +00002//
Reid Spencerde4cedc2004-12-13 03:00:28 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanf976c852005-04-21 22:55:34 +00007//
Reid Spencerde4cedc2004-12-13 03:00:28 +00008//===----------------------------------------------------------------------===//
9//
Reid Spencer903f21d2004-12-13 03:50:50 +000010// This file contains basic Linker functionality that all usages will need.
Reid Spencerde4cedc2004-12-13 03:00:28 +000011//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/Linker.h"
15#include "llvm/Module.h"
Chris Lattner1a019e52007-05-06 06:02:13 +000016#include "llvm/Bitcode/ReaderWriter.h"
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000017#include "llvm/Support/Path.h"
Chris Lattner1a019e52007-05-06 06:02:13 +000018#include "llvm/Support/MemoryBuffer.h"
Daniel Dunbar92ccf702009-07-25 06:02:13 +000019#include "llvm/Support/raw_ostream.h"
Chris Lattner74382b72009-08-23 22:45:37 +000020#include "llvm/Config/config.h"
Michael J. Spencer333fb042010-12-09 17:36:48 +000021#include "llvm/Support/system_error.h"
Reid Spencerde4cedc2004-12-13 03:00:28 +000022using namespace llvm;
23
Daniel Dunbar2928c832009-11-06 10:58:06 +000024Linker::Linker(StringRef progname, StringRef modname,
25 LLVMContext& C, unsigned flags):
Owen Anderson8b477ed2009-07-01 16:58:40 +000026 Context(C),
27 Composite(new Module(modname, C)),
28 LibPaths(),
29 Flags(flags),
30 Error(),
31 ProgramName(progname) { }
Reid Spencerde4cedc2004-12-13 03:00:28 +000032
Daniel Dunbar2928c832009-11-06 10:58:06 +000033Linker::Linker(StringRef progname, Module* aModule, unsigned flags) :
Owen Anderson8b477ed2009-07-01 16:58:40 +000034 Context(aModule->getContext()),
35 Composite(aModule),
36 LibPaths(),
37 Flags(flags),
38 Error(),
39 ProgramName(progname) { }
Reid Spencerde4cedc2004-12-13 03:00:28 +000040
41Linker::~Linker() {
42 delete Composite;
43}
44
Misha Brukmanf976c852005-04-21 22:55:34 +000045bool
Daniel Dunbar2928c832009-11-06 10:58:06 +000046Linker::error(StringRef message) {
Reid Spencerde4cedc2004-12-13 03:00:28 +000047 Error = message;
Bill Wendling41edad72006-11-27 10:09:12 +000048 if (!(Flags&QuietErrors))
Daniel Dunbar92ccf702009-07-25 06:02:13 +000049 errs() << ProgramName << ": error: " << message << "\n";
Reid Spencerde4cedc2004-12-13 03:00:28 +000050 return true;
51}
52
53bool
Daniel Dunbar2928c832009-11-06 10:58:06 +000054Linker::warning(StringRef message) {
Reid Spencerde4cedc2004-12-13 03:00:28 +000055 Error = message;
Dan Gohman5d9759b2008-10-25 17:57:20 +000056 if (!(Flags&QuietWarnings))
Daniel Dunbar92ccf702009-07-25 06:02:13 +000057 errs() << ProgramName << ": warning: " << message << "\n";
Reid Spencerde4cedc2004-12-13 03:00:28 +000058 return false;
59}
60
61void
Daniel Dunbar2928c832009-11-06 10:58:06 +000062Linker::verbose(StringRef message) {
Bill Wendling41edad72006-11-27 10:09:12 +000063 if (Flags&Verbose)
David Greene2abc1ec2010-01-05 01:55:22 +000064 errs() << " " << message << "\n";
Reid Spencerde4cedc2004-12-13 03:00:28 +000065}
66
67void
68Linker::addPath(const sys::Path& path) {
Reid Spencerde4cedc2004-12-13 03:00:28 +000069 LibPaths.push_back(path);
70}
71
72void
73Linker::addPaths(const std::vector<std::string>& paths) {
Chris Lattner74382b72009-08-23 22:45:37 +000074 for (unsigned i = 0, e = paths.size(); i != e; ++i)
75 LibPaths.push_back(sys::Path(paths[i]));
Reid Spencerde4cedc2004-12-13 03:00:28 +000076}
77
78void
79Linker::addSystemPaths() {
Gabor Greifa99be512007-07-05 17:07:56 +000080 sys::Path::GetBitcodeLibraryPaths(LibPaths);
Reid Spencerde4cedc2004-12-13 03:00:28 +000081 LibPaths.insert(LibPaths.begin(),sys::Path("./"));
82}
83
84Module*
85Linker::releaseModule() {
86 Module* result = Composite;
Reid Spencerde4cedc2004-12-13 03:00:28 +000087 LibPaths.clear();
88 Error.clear();
Reid Spencer903f21d2004-12-13 03:50:50 +000089 Composite = 0;
Reid Spencerde4cedc2004-12-13 03:00:28 +000090 Flags = 0;
91 return result;
92}
93
Gabor Greifa99be512007-07-05 17:07:56 +000094// LoadObject - Read in and parse the bitcode file named by FN and return the
Misha Brukmanf976c852005-04-21 22:55:34 +000095// module it contains (wrapped in an auto_ptr), or auto_ptr<Module>() and set
Reid Spencerde4cedc2004-12-13 03:00:28 +000096// Error if an error occurs.
Misha Brukmanf976c852005-04-21 22:55:34 +000097std::auto_ptr<Module>
Reid Spencerde4cedc2004-12-13 03:00:28 +000098Linker::LoadObject(const sys::Path &FN) {
99 std::string ParseErrorMessage;
Chris Lattner1a019e52007-05-06 06:02:13 +0000100 Module *Result = 0;
Mikhail Glushenkov572ec1f2010-11-02 20:32:52 +0000101
Michael J. Spencer3ff95632010-12-16 03:29:14 +0000102 OwningPtr<MemoryBuffer> Buffer;
103 if (error_code ec = MemoryBuffer::getFileOrSTDIN(FN.c_str(), Buffer))
Michael J. Spencer333fb042010-12-09 17:36:48 +0000104 ParseErrorMessage = "Error reading file '" + FN.str() + "'" + ": "
105 + ec.message();
Michael J. Spencer3ff95632010-12-16 03:29:14 +0000106 else
107 Result = ParseBitcodeFile(Buffer.get(), Context, &ParseErrorMessage);
Mikhail Glushenkov572ec1f2010-11-02 20:32:52 +0000108
Misha Brukmanf976c852005-04-21 22:55:34 +0000109 if (Result)
Reid Spencerde4cedc2004-12-13 03:00:28 +0000110 return std::auto_ptr<Module>(Result);
Chris Lattner74382b72009-08-23 22:45:37 +0000111 Error = "Bitcode file '" + FN.str() + "' could not be loaded";
Misha Brukmanf976c852005-04-21 22:55:34 +0000112 if (ParseErrorMessage.size())
Reid Spencerde4cedc2004-12-13 03:00:28 +0000113 Error += ": " + ParseErrorMessage;
114 return std::auto_ptr<Module>();
115}
116
Misha Brukmanf976c852005-04-21 22:55:34 +0000117// IsLibrary - Determine if "Name" is a library in "Directory". Return
Reid Spencer903f21d2004-12-13 03:50:50 +0000118// a non-empty sys::Path if its found, an empty one otherwise.
Daniel Dunbar2928c832009-11-06 10:58:06 +0000119static inline sys::Path IsLibrary(StringRef Name,
Daniel Dunbar92ccf702009-07-25 06:02:13 +0000120 const sys::Path &Directory) {
Reid Spencerde4cedc2004-12-13 03:00:28 +0000121
Reid Spencerde4cedc2004-12-13 03:00:28 +0000122 sys::Path FullPath(Directory);
Reid Spencerde4cedc2004-12-13 03:00:28 +0000123
Chris Lattner501d5292006-07-28 22:52:11 +0000124 // Try the libX.a form
Daniel Dunbar92ccf702009-07-25 06:02:13 +0000125 FullPath.appendComponent(("lib" + Name).str());
Chris Lattner501d5292006-07-28 22:52:11 +0000126 FullPath.appendSuffix("a");
127 if (FullPath.isArchive())
128 return FullPath;
Reid Spencerde4cedc2004-12-13 03:00:28 +0000129
Chris Lattner501d5292006-07-28 22:52:11 +0000130 // Try the libX.bca form
131 FullPath.eraseSuffix();
132 FullPath.appendSuffix("bca");
133 if (FullPath.isArchive())
134 return FullPath;
Reid Spencerde4cedc2004-12-13 03:00:28 +0000135
Chris Lattner501d5292006-07-28 22:52:11 +0000136 // Try the libX.so (or .dylib) form
137 FullPath.eraseSuffix();
Mikhail Glushenkovc8aef4b2010-11-02 20:32:59 +0000138 FullPath.appendSuffix(sys::Path::GetDLLSuffix());
Chris Lattner501d5292006-07-28 22:52:11 +0000139 if (FullPath.isDynamicLibrary()) // Native shared library?
140 return FullPath;
Chris Lattner1a019e52007-05-06 06:02:13 +0000141 if (FullPath.isBitcodeFile()) // .so file containing bitcode?
142 return FullPath;
Reid Spencerde4cedc2004-12-13 03:00:28 +0000143
Ivan Krasin5f2318e2011-09-20 22:52:35 +0000144 // Try libX form, to make it possible to add dependency on the
145 // specific version of .so, like liblzma.so.1.0.0
146 FullPath.eraseSuffix();
147 if (FullPath.isDynamicLibrary()) // Native shared library?
148 return FullPath;
149 if (FullPath.isBitcodeFile()) // .so file containing bitcode?
150 return FullPath;
151
Chris Lattner501d5292006-07-28 22:52:11 +0000152 // Not found .. fall through
Reid Spencerdd04df02005-07-07 23:21:43 +0000153
154 // Indicate that the library was not found in the directory.
Reid Spencerde4cedc2004-12-13 03:00:28 +0000155 FullPath.clear();
156 return FullPath;
157}
158
159/// FindLib - Try to convert Filename into the name of a file that we can open,
160/// if it does not already name a file we can open, by first trying to open
161/// Filename, then libFilename.[suffix] for each of a set of several common
Misha Brukmanf976c852005-04-21 22:55:34 +0000162/// library suffixes, in each of the directories in LibPaths. Returns an empty
Reid Spencer903f21d2004-12-13 03:50:50 +0000163/// Path if no matching file can be found.
Reid Spencerde4cedc2004-12-13 03:00:28 +0000164///
Misha Brukmanf976c852005-04-21 22:55:34 +0000165sys::Path
Daniel Dunbar2928c832009-11-06 10:58:06 +0000166Linker::FindLib(StringRef Filename) {
Reid Spencerde4cedc2004-12-13 03:00:28 +0000167 // Determine if the pathname can be found as it stands.
168 sys::Path FilePath(Filename);
Reid Spencerc7f08322005-07-07 18:21:42 +0000169 if (FilePath.canRead() &&
Reid Spencerde4cedc2004-12-13 03:00:28 +0000170 (FilePath.isArchive() || FilePath.isDynamicLibrary()))
171 return FilePath;
172
173 // Iterate over the directories in Paths to see if we can find the library
174 // there.
175 for (unsigned Index = 0; Index != LibPaths.size(); ++Index) {
176 sys::Path Directory(LibPaths[Index]);
Daniel Dunbar92ccf702009-07-25 06:02:13 +0000177 sys::Path FullPath = IsLibrary(Filename, Directory);
Reid Spencerde4cedc2004-12-13 03:00:28 +0000178 if (!FullPath.isEmpty())
179 return FullPath;
180 }
181 return sys::Path();
182}