blob: 42d4e724db49da5c05ae556b04dafb30b2944c0b [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"
Reid Spencera834f5d2004-12-16 19:19:24 +000017#include "llvm/Config/config.h"
Chris Lattner1a019e52007-05-06 06:02:13 +000018#include "llvm/Support/MemoryBuffer.h"
Bill Wendling41edad72006-11-27 10:09:12 +000019#include "llvm/Support/Streams.h"
Reid Spencerde4cedc2004-12-13 03:00:28 +000020using namespace llvm;
21
Chris Lattner038112a2008-04-01 18:04:03 +000022Linker::Linker(const std::string& progname, const std::string& modname,
Owen Anderson31895e72009-07-01 21:22:36 +000023 const LLVMContext& C, unsigned flags):
Owen Anderson8b477ed2009-07-01 16:58:40 +000024 Context(C),
25 Composite(new Module(modname, C)),
26 LibPaths(),
27 Flags(flags),
28 Error(),
29 ProgramName(progname) { }
Reid Spencerde4cedc2004-12-13 03:00:28 +000030
Owen Anderson8b477ed2009-07-01 16:58:40 +000031Linker::Linker(const std::string& progname, Module* aModule, unsigned flags) :
32 Context(aModule->getContext()),
33 Composite(aModule),
34 LibPaths(),
35 Flags(flags),
36 Error(),
37 ProgramName(progname) { }
Reid Spencerde4cedc2004-12-13 03:00:28 +000038
39Linker::~Linker() {
40 delete Composite;
41}
42
Misha Brukmanf976c852005-04-21 22:55:34 +000043bool
Reid Spencerde4cedc2004-12-13 03:00:28 +000044Linker::error(const std::string& message) {
45 Error = message;
Bill Wendling41edad72006-11-27 10:09:12 +000046 if (!(Flags&QuietErrors))
Bill Wendlinge8156192006-12-07 01:30:32 +000047 cerr << ProgramName << ": error: " << message << "\n";
Reid Spencerde4cedc2004-12-13 03:00:28 +000048 return true;
49}
50
51bool
52Linker::warning(const std::string& message) {
53 Error = message;
Dan Gohman5d9759b2008-10-25 17:57:20 +000054 if (!(Flags&QuietWarnings))
Bill Wendlinge8156192006-12-07 01:30:32 +000055 cerr << ProgramName << ": warning: " << message << "\n";
Reid Spencerde4cedc2004-12-13 03:00:28 +000056 return false;
57}
58
59void
60Linker::verbose(const std::string& message) {
Bill Wendling41edad72006-11-27 10:09:12 +000061 if (Flags&Verbose)
Bill Wendlinge8156192006-12-07 01:30:32 +000062 cerr << " " << message << "\n";
Reid Spencerde4cedc2004-12-13 03:00:28 +000063}
64
65void
66Linker::addPath(const sys::Path& path) {
Reid Spencerde4cedc2004-12-13 03:00:28 +000067 LibPaths.push_back(path);
68}
69
70void
71Linker::addPaths(const std::vector<std::string>& paths) {
Reid Spencer903f21d2004-12-13 03:50:50 +000072 for (unsigned i = 0; i != paths.size(); ++i) {
Reid Spencerde4cedc2004-12-13 03:00:28 +000073 sys::Path aPath;
Reid Spencerdd04df02005-07-07 23:21:43 +000074 aPath.set(paths[i]);
Reid Spencerde4cedc2004-12-13 03:00:28 +000075 LibPaths.push_back(aPath);
76 }
77}
78
79void
80Linker::addSystemPaths() {
Gabor Greifa99be512007-07-05 17:07:56 +000081 sys::Path::GetBitcodeLibraryPaths(LibPaths);
Reid Spencerde4cedc2004-12-13 03:00:28 +000082 LibPaths.insert(LibPaths.begin(),sys::Path("./"));
83}
84
85Module*
86Linker::releaseModule() {
87 Module* result = Composite;
Reid Spencerde4cedc2004-12-13 03:00:28 +000088 LibPaths.clear();
89 Error.clear();
Reid Spencer903f21d2004-12-13 03:50:50 +000090 Composite = 0;
Reid Spencerde4cedc2004-12-13 03:00:28 +000091 Flags = 0;
92 return result;
93}
94
Gabor Greifa99be512007-07-05 17:07:56 +000095// LoadObject - Read in and parse the bitcode file named by FN and return the
Misha Brukmanf976c852005-04-21 22:55:34 +000096// module it contains (wrapped in an auto_ptr), or auto_ptr<Module>() and set
Reid Spencerde4cedc2004-12-13 03:00:28 +000097// Error if an error occurs.
Misha Brukmanf976c852005-04-21 22:55:34 +000098std::auto_ptr<Module>
Reid Spencerde4cedc2004-12-13 03:00:28 +000099Linker::LoadObject(const sys::Path &FN) {
100 std::string ParseErrorMessage;
Chris Lattner1a019e52007-05-06 06:02:13 +0000101 Module *Result = 0;
102
103 const std::string &FNS = FN.toString();
Chris Lattner038112a2008-04-01 18:04:03 +0000104 std::auto_ptr<MemoryBuffer> Buffer(MemoryBuffer::getFileOrSTDIN(FNS.c_str()));
Chris Lattner4bcca0f2007-05-06 09:29:13 +0000105 if (Buffer.get())
Owen Anderson8b477ed2009-07-01 16:58:40 +0000106 Result = ParseBitcodeFile(Buffer.get(), Context, &ParseErrorMessage);
Chris Lattner4bcca0f2007-05-06 09:29:13 +0000107 else
108 ParseErrorMessage = "Error reading file '" + FNS + "'";
Chris Lattner1a019e52007-05-06 06:02:13 +0000109
Misha Brukmanf976c852005-04-21 22:55:34 +0000110 if (Result)
Reid Spencerde4cedc2004-12-13 03:00:28 +0000111 return std::auto_ptr<Module>(Result);
Gabor Greifa99be512007-07-05 17:07:56 +0000112 Error = "Bitcode file '" + FN.toString() + "' could not be loaded";
Misha Brukmanf976c852005-04-21 22:55:34 +0000113 if (ParseErrorMessage.size())
Reid Spencerde4cedc2004-12-13 03:00:28 +0000114 Error += ": " + ParseErrorMessage;
115 return std::auto_ptr<Module>();
116}
117
Misha Brukmanf976c852005-04-21 22:55:34 +0000118// IsLibrary - Determine if "Name" is a library in "Directory". Return
Reid Spencer903f21d2004-12-13 03:50:50 +0000119// a non-empty sys::Path if its found, an empty one otherwise.
Misha Brukmanf976c852005-04-21 22:55:34 +0000120static inline sys::Path IsLibrary(const std::string& Name,
Reid Spencerde4cedc2004-12-13 03:00:28 +0000121 const sys::Path& Directory) {
122
Reid Spencerde4cedc2004-12-13 03:00:28 +0000123 sys::Path FullPath(Directory);
Reid Spencerde4cedc2004-12-13 03:00:28 +0000124
Chris Lattner501d5292006-07-28 22:52:11 +0000125 // Try the libX.a form
126 FullPath.appendComponent("lib" + Name);
127 FullPath.appendSuffix("a");
128 if (FullPath.isArchive())
129 return FullPath;
Reid Spencerde4cedc2004-12-13 03:00:28 +0000130
Chris Lattner501d5292006-07-28 22:52:11 +0000131 // Try the libX.bca form
132 FullPath.eraseSuffix();
133 FullPath.appendSuffix("bca");
134 if (FullPath.isArchive())
135 return FullPath;
Reid Spencerde4cedc2004-12-13 03:00:28 +0000136
Chris Lattner501d5292006-07-28 22:52:11 +0000137 // Try the libX.so (or .dylib) form
138 FullPath.eraseSuffix();
139 FullPath.appendSuffix(&(LTDL_SHLIB_EXT[1]));
140 if (FullPath.isDynamicLibrary()) // Native shared library?
141 return FullPath;
Chris Lattner1a019e52007-05-06 06:02:13 +0000142 if (FullPath.isBitcodeFile()) // .so file containing bitcode?
143 return FullPath;
Reid Spencerde4cedc2004-12-13 03:00:28 +0000144
Chris Lattner501d5292006-07-28 22:52:11 +0000145 // Not found .. fall through
Reid Spencerdd04df02005-07-07 23:21:43 +0000146
147 // Indicate that the library was not found in the directory.
Reid Spencerde4cedc2004-12-13 03:00:28 +0000148 FullPath.clear();
149 return FullPath;
150}
151
152/// FindLib - Try to convert Filename into the name of a file that we can open,
153/// if it does not already name a file we can open, by first trying to open
154/// Filename, then libFilename.[suffix] for each of a set of several common
Misha Brukmanf976c852005-04-21 22:55:34 +0000155/// library suffixes, in each of the directories in LibPaths. Returns an empty
Reid Spencer903f21d2004-12-13 03:50:50 +0000156/// Path if no matching file can be found.
Reid Spencerde4cedc2004-12-13 03:00:28 +0000157///
Misha Brukmanf976c852005-04-21 22:55:34 +0000158sys::Path
John Criswell43da9c62006-01-17 22:01:57 +0000159Linker::FindLib(const std::string &Filename) {
Reid Spencerde4cedc2004-12-13 03:00:28 +0000160 // Determine if the pathname can be found as it stands.
161 sys::Path FilePath(Filename);
Reid Spencerc7f08322005-07-07 18:21:42 +0000162 if (FilePath.canRead() &&
Reid Spencerde4cedc2004-12-13 03:00:28 +0000163 (FilePath.isArchive() || FilePath.isDynamicLibrary()))
164 return FilePath;
165
166 // Iterate over the directories in Paths to see if we can find the library
167 // there.
168 for (unsigned Index = 0; Index != LibPaths.size(); ++Index) {
169 sys::Path Directory(LibPaths[Index]);
170 sys::Path FullPath = IsLibrary(Filename,Directory);
171 if (!FullPath.isEmpty())
172 return FullPath;
173 }
174 return sys::Path();
175}