blob: 3d24804fbd0e4e43c3fd30a2d82897563b7f584b [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"
David Greenec2e09742010-01-05 01:27:53 +000018#include "llvm/Support/Debug.h"
Chris Lattner1a019e52007-05-06 06:02:13 +000019#include "llvm/Support/MemoryBuffer.h"
Daniel Dunbar92ccf702009-07-25 06:02:13 +000020#include "llvm/Support/raw_ostream.h"
Chris Lattner74382b72009-08-23 22:45:37 +000021#include "llvm/Config/config.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 Greenec2e09742010-01-05 01:27:53 +000064 dbgs() << " " << 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;
101
Chris Lattner74382b72009-08-23 22:45:37 +0000102 std::auto_ptr<MemoryBuffer> Buffer(MemoryBuffer::getFileOrSTDIN(FN.c_str()));
Chris Lattner4bcca0f2007-05-06 09:29:13 +0000103 if (Buffer.get())
Owen Anderson8b477ed2009-07-01 16:58:40 +0000104 Result = ParseBitcodeFile(Buffer.get(), Context, &ParseErrorMessage);
Chris Lattner4bcca0f2007-05-06 09:29:13 +0000105 else
Chris Lattner74382b72009-08-23 22:45:37 +0000106 ParseErrorMessage = "Error reading file '" + FN.str() + "'";
Chris Lattner1a019e52007-05-06 06:02:13 +0000107
Misha Brukmanf976c852005-04-21 22:55:34 +0000108 if (Result)
Reid Spencerde4cedc2004-12-13 03:00:28 +0000109 return std::auto_ptr<Module>(Result);
Chris Lattner74382b72009-08-23 22:45:37 +0000110 Error = "Bitcode file '" + FN.str() + "' could not be loaded";
Misha Brukmanf976c852005-04-21 22:55:34 +0000111 if (ParseErrorMessage.size())
Reid Spencerde4cedc2004-12-13 03:00:28 +0000112 Error += ": " + ParseErrorMessage;
113 return std::auto_ptr<Module>();
114}
115
Misha Brukmanf976c852005-04-21 22:55:34 +0000116// IsLibrary - Determine if "Name" is a library in "Directory". Return
Reid Spencer903f21d2004-12-13 03:50:50 +0000117// a non-empty sys::Path if its found, an empty one otherwise.
Daniel Dunbar2928c832009-11-06 10:58:06 +0000118static inline sys::Path IsLibrary(StringRef Name,
Daniel Dunbar92ccf702009-07-25 06:02:13 +0000119 const sys::Path &Directory) {
Reid Spencerde4cedc2004-12-13 03:00:28 +0000120
Reid Spencerde4cedc2004-12-13 03:00:28 +0000121 sys::Path FullPath(Directory);
Reid Spencerde4cedc2004-12-13 03:00:28 +0000122
Chris Lattner501d5292006-07-28 22:52:11 +0000123 // Try the libX.a form
Daniel Dunbar92ccf702009-07-25 06:02:13 +0000124 FullPath.appendComponent(("lib" + Name).str());
Chris Lattner501d5292006-07-28 22:52:11 +0000125 FullPath.appendSuffix("a");
126 if (FullPath.isArchive())
127 return FullPath;
Reid Spencerde4cedc2004-12-13 03:00:28 +0000128
Chris Lattner501d5292006-07-28 22:52:11 +0000129 // Try the libX.bca form
130 FullPath.eraseSuffix();
131 FullPath.appendSuffix("bca");
132 if (FullPath.isArchive())
133 return FullPath;
Reid Spencerde4cedc2004-12-13 03:00:28 +0000134
Chris Lattner501d5292006-07-28 22:52:11 +0000135 // Try the libX.so (or .dylib) form
136 FullPath.eraseSuffix();
137 FullPath.appendSuffix(&(LTDL_SHLIB_EXT[1]));
138 if (FullPath.isDynamicLibrary()) // Native shared library?
139 return FullPath;
Chris Lattner1a019e52007-05-06 06:02:13 +0000140 if (FullPath.isBitcodeFile()) // .so file containing bitcode?
141 return FullPath;
Reid Spencerde4cedc2004-12-13 03:00:28 +0000142
Chris Lattner501d5292006-07-28 22:52:11 +0000143 // Not found .. fall through
Reid Spencerdd04df02005-07-07 23:21:43 +0000144
145 // Indicate that the library was not found in the directory.
Reid Spencerde4cedc2004-12-13 03:00:28 +0000146 FullPath.clear();
147 return FullPath;
148}
149
150/// FindLib - Try to convert Filename into the name of a file that we can open,
151/// if it does not already name a file we can open, by first trying to open
152/// Filename, then libFilename.[suffix] for each of a set of several common
Misha Brukmanf976c852005-04-21 22:55:34 +0000153/// library suffixes, in each of the directories in LibPaths. Returns an empty
Reid Spencer903f21d2004-12-13 03:50:50 +0000154/// Path if no matching file can be found.
Reid Spencerde4cedc2004-12-13 03:00:28 +0000155///
Misha Brukmanf976c852005-04-21 22:55:34 +0000156sys::Path
Daniel Dunbar2928c832009-11-06 10:58:06 +0000157Linker::FindLib(StringRef Filename) {
Reid Spencerde4cedc2004-12-13 03:00:28 +0000158 // Determine if the pathname can be found as it stands.
159 sys::Path FilePath(Filename);
Reid Spencerc7f08322005-07-07 18:21:42 +0000160 if (FilePath.canRead() &&
Reid Spencerde4cedc2004-12-13 03:00:28 +0000161 (FilePath.isArchive() || FilePath.isDynamicLibrary()))
162 return FilePath;
163
164 // Iterate over the directories in Paths to see if we can find the library
165 // there.
166 for (unsigned Index = 0; Index != LibPaths.size(); ++Index) {
167 sys::Path Directory(LibPaths[Index]);
Daniel Dunbar92ccf702009-07-25 06:02:13 +0000168 sys::Path FullPath = IsLibrary(Filename, Directory);
Reid Spencerde4cedc2004-12-13 03:00:28 +0000169 if (!FullPath.isEmpty())
170 return FullPath;
171 }
172 return sys::Path();
173}