blob: 32aa0f9011213528e676580372b242c44a11c01f [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"
Chris Lattner74382b72009-08-23 22:45:37 +000017#include "llvm/System/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"
Reid Spencerde4cedc2004-12-13 03:00:28 +000021using namespace llvm;
22
Daniel Dunbar2928c832009-11-06 10:58:06 +000023Linker::Linker(StringRef progname, StringRef modname,
24 LLVMContext& C, unsigned flags):
Owen Anderson8b477ed2009-07-01 16:58:40 +000025 Context(C),
26 Composite(new Module(modname, C)),
27 LibPaths(),
28 Flags(flags),
29 Error(),
30 ProgramName(progname) { }
Reid Spencerde4cedc2004-12-13 03:00:28 +000031
Daniel Dunbar2928c832009-11-06 10:58:06 +000032Linker::Linker(StringRef progname, Module* aModule, unsigned flags) :
Owen Anderson8b477ed2009-07-01 16:58:40 +000033 Context(aModule->getContext()),
34 Composite(aModule),
35 LibPaths(),
36 Flags(flags),
37 Error(),
38 ProgramName(progname) { }
Reid Spencerde4cedc2004-12-13 03:00:28 +000039
40Linker::~Linker() {
41 delete Composite;
42}
43
Misha Brukmanf976c852005-04-21 22:55:34 +000044bool
Daniel Dunbar2928c832009-11-06 10:58:06 +000045Linker::error(StringRef message) {
Reid Spencerde4cedc2004-12-13 03:00:28 +000046 Error = message;
Bill Wendling41edad72006-11-27 10:09:12 +000047 if (!(Flags&QuietErrors))
Daniel Dunbar92ccf702009-07-25 06:02:13 +000048 errs() << ProgramName << ": error: " << message << "\n";
Reid Spencerde4cedc2004-12-13 03:00:28 +000049 return true;
50}
51
52bool
Daniel Dunbar2928c832009-11-06 10:58:06 +000053Linker::warning(StringRef message) {
Reid Spencerde4cedc2004-12-13 03:00:28 +000054 Error = message;
Dan Gohman5d9759b2008-10-25 17:57:20 +000055 if (!(Flags&QuietWarnings))
Daniel Dunbar92ccf702009-07-25 06:02:13 +000056 errs() << ProgramName << ": warning: " << message << "\n";
Reid Spencerde4cedc2004-12-13 03:00:28 +000057 return false;
58}
59
60void
Daniel Dunbar2928c832009-11-06 10:58:06 +000061Linker::verbose(StringRef message) {
Bill Wendling41edad72006-11-27 10:09:12 +000062 if (Flags&Verbose)
Daniel Dunbar92ccf702009-07-25 06:02:13 +000063 errs() << " " << message << "\n";
Reid Spencerde4cedc2004-12-13 03:00:28 +000064}
65
66void
67Linker::addPath(const sys::Path& path) {
Reid Spencerde4cedc2004-12-13 03:00:28 +000068 LibPaths.push_back(path);
69}
70
71void
72Linker::addPaths(const std::vector<std::string>& paths) {
Chris Lattner74382b72009-08-23 22:45:37 +000073 for (unsigned i = 0, e = paths.size(); i != e; ++i)
74 LibPaths.push_back(sys::Path(paths[i]));
Reid Spencerde4cedc2004-12-13 03:00:28 +000075}
76
77void
78Linker::addSystemPaths() {
Gabor Greifa99be512007-07-05 17:07:56 +000079 sys::Path::GetBitcodeLibraryPaths(LibPaths);
Reid Spencerde4cedc2004-12-13 03:00:28 +000080 LibPaths.insert(LibPaths.begin(),sys::Path("./"));
81}
82
83Module*
84Linker::releaseModule() {
85 Module* result = Composite;
Reid Spencerde4cedc2004-12-13 03:00:28 +000086 LibPaths.clear();
87 Error.clear();
Reid Spencer903f21d2004-12-13 03:50:50 +000088 Composite = 0;
Reid Spencerde4cedc2004-12-13 03:00:28 +000089 Flags = 0;
90 return result;
91}
92
Gabor Greifa99be512007-07-05 17:07:56 +000093// LoadObject - Read in and parse the bitcode file named by FN and return the
Misha Brukmanf976c852005-04-21 22:55:34 +000094// module it contains (wrapped in an auto_ptr), or auto_ptr<Module>() and set
Reid Spencerde4cedc2004-12-13 03:00:28 +000095// Error if an error occurs.
Misha Brukmanf976c852005-04-21 22:55:34 +000096std::auto_ptr<Module>
Reid Spencerde4cedc2004-12-13 03:00:28 +000097Linker::LoadObject(const sys::Path &FN) {
98 std::string ParseErrorMessage;
Chris Lattner1a019e52007-05-06 06:02:13 +000099 Module *Result = 0;
100
Chris Lattner74382b72009-08-23 22:45:37 +0000101 std::auto_ptr<MemoryBuffer> Buffer(MemoryBuffer::getFileOrSTDIN(FN.c_str()));
Chris Lattner4bcca0f2007-05-06 09:29:13 +0000102 if (Buffer.get())
Owen Anderson8b477ed2009-07-01 16:58:40 +0000103 Result = ParseBitcodeFile(Buffer.get(), Context, &ParseErrorMessage);
Chris Lattner4bcca0f2007-05-06 09:29:13 +0000104 else
Chris Lattner74382b72009-08-23 22:45:37 +0000105 ParseErrorMessage = "Error reading file '" + FN.str() + "'";
Chris Lattner1a019e52007-05-06 06:02:13 +0000106
Misha Brukmanf976c852005-04-21 22:55:34 +0000107 if (Result)
Reid Spencerde4cedc2004-12-13 03:00:28 +0000108 return std::auto_ptr<Module>(Result);
Chris Lattner74382b72009-08-23 22:45:37 +0000109 Error = "Bitcode file '" + FN.str() + "' could not be loaded";
Misha Brukmanf976c852005-04-21 22:55:34 +0000110 if (ParseErrorMessage.size())
Reid Spencerde4cedc2004-12-13 03:00:28 +0000111 Error += ": " + ParseErrorMessage;
112 return std::auto_ptr<Module>();
113}
114
Misha Brukmanf976c852005-04-21 22:55:34 +0000115// IsLibrary - Determine if "Name" is a library in "Directory". Return
Reid Spencer903f21d2004-12-13 03:50:50 +0000116// a non-empty sys::Path if its found, an empty one otherwise.
Daniel Dunbar2928c832009-11-06 10:58:06 +0000117static inline sys::Path IsLibrary(StringRef Name,
Daniel Dunbar92ccf702009-07-25 06:02:13 +0000118 const sys::Path &Directory) {
Reid Spencerde4cedc2004-12-13 03:00:28 +0000119
Reid Spencerde4cedc2004-12-13 03:00:28 +0000120 sys::Path FullPath(Directory);
Reid Spencerde4cedc2004-12-13 03:00:28 +0000121
Chris Lattner501d5292006-07-28 22:52:11 +0000122 // Try the libX.a form
Daniel Dunbar92ccf702009-07-25 06:02:13 +0000123 FullPath.appendComponent(("lib" + Name).str());
Chris Lattner501d5292006-07-28 22:52:11 +0000124 FullPath.appendSuffix("a");
125 if (FullPath.isArchive())
126 return FullPath;
Reid Spencerde4cedc2004-12-13 03:00:28 +0000127
Chris Lattner501d5292006-07-28 22:52:11 +0000128 // Try the libX.bca form
129 FullPath.eraseSuffix();
130 FullPath.appendSuffix("bca");
131 if (FullPath.isArchive())
132 return FullPath;
Reid Spencerde4cedc2004-12-13 03:00:28 +0000133
Chris Lattner501d5292006-07-28 22:52:11 +0000134 // Try the libX.so (or .dylib) form
135 FullPath.eraseSuffix();
136 FullPath.appendSuffix(&(LTDL_SHLIB_EXT[1]));
137 if (FullPath.isDynamicLibrary()) // Native shared library?
138 return FullPath;
Chris Lattner1a019e52007-05-06 06:02:13 +0000139 if (FullPath.isBitcodeFile()) // .so file containing bitcode?
140 return FullPath;
Reid Spencerde4cedc2004-12-13 03:00:28 +0000141
Chris Lattner501d5292006-07-28 22:52:11 +0000142 // Not found .. fall through
Reid Spencerdd04df02005-07-07 23:21:43 +0000143
144 // Indicate that the library was not found in the directory.
Reid Spencerde4cedc2004-12-13 03:00:28 +0000145 FullPath.clear();
146 return FullPath;
147}
148
149/// FindLib - Try to convert Filename into the name of a file that we can open,
150/// if it does not already name a file we can open, by first trying to open
151/// Filename, then libFilename.[suffix] for each of a set of several common
Misha Brukmanf976c852005-04-21 22:55:34 +0000152/// library suffixes, in each of the directories in LibPaths. Returns an empty
Reid Spencer903f21d2004-12-13 03:50:50 +0000153/// Path if no matching file can be found.
Reid Spencerde4cedc2004-12-13 03:00:28 +0000154///
Misha Brukmanf976c852005-04-21 22:55:34 +0000155sys::Path
Daniel Dunbar2928c832009-11-06 10:58:06 +0000156Linker::FindLib(StringRef Filename) {
Reid Spencerde4cedc2004-12-13 03:00:28 +0000157 // Determine if the pathname can be found as it stands.
158 sys::Path FilePath(Filename);
Reid Spencerc7f08322005-07-07 18:21:42 +0000159 if (FilePath.canRead() &&
Reid Spencerde4cedc2004-12-13 03:00:28 +0000160 (FilePath.isArchive() || FilePath.isDynamicLibrary()))
161 return FilePath;
162
163 // Iterate over the directories in Paths to see if we can find the library
164 // there.
165 for (unsigned Index = 0; Index != LibPaths.size(); ++Index) {
166 sys::Path Directory(LibPaths[Index]);
Daniel Dunbar92ccf702009-07-25 06:02:13 +0000167 sys::Path FullPath = IsLibrary(Filename, Directory);
Reid Spencerde4cedc2004-12-13 03:00:28 +0000168 if (!FullPath.isEmpty())
169 return FullPath;
170 }
171 return sys::Path();
172}