blob: aef79d08f423ef3e373443b561b185dcc54aa378 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===- lib/Linker/Linker.cpp - Basic Linker functionality ----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file contains basic Linker functionality that all usages will need.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/Linker.h"
15#include "llvm/Module.h"
16#include "llvm/Bitcode/ReaderWriter.h"
Chris Lattnerb1aa85b2009-08-23 22:45:37 +000017#include "llvm/System/Path.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000018#include "llvm/Support/MemoryBuffer.h"
Daniel Dunbara49de6f2009-07-25 06:02:13 +000019#include "llvm/Support/raw_ostream.h"
Chris Lattnerb1aa85b2009-08-23 22:45:37 +000020#include "llvm/Config/config.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000021using namespace llvm;
22
Daniel Dunbara49de6f2009-07-25 06:02:13 +000023Linker::Linker(const StringRef &progname, const StringRef &modname,
Owen Anderson92adb182009-07-01 23:13:44 +000024 LLVMContext& C, unsigned flags):
Owen Anderson25209b42009-07-01 16:58:40 +000025 Context(C),
26 Composite(new Module(modname, C)),
27 LibPaths(),
28 Flags(flags),
29 Error(),
30 ProgramName(progname) { }
Dan Gohmanf17a25c2007-07-18 16:29:46 +000031
Daniel Dunbara49de6f2009-07-25 06:02:13 +000032Linker::Linker(const StringRef &progname, Module* aModule, unsigned flags) :
Owen Anderson25209b42009-07-01 16:58:40 +000033 Context(aModule->getContext()),
34 Composite(aModule),
35 LibPaths(),
36 Flags(flags),
37 Error(),
38 ProgramName(progname) { }
Dan Gohmanf17a25c2007-07-18 16:29:46 +000039
40Linker::~Linker() {
41 delete Composite;
42}
43
44bool
Daniel Dunbara49de6f2009-07-25 06:02:13 +000045Linker::error(const StringRef &message) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000046 Error = message;
47 if (!(Flags&QuietErrors))
Daniel Dunbara49de6f2009-07-25 06:02:13 +000048 errs() << ProgramName << ": error: " << message << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000049 return true;
50}
51
52bool
Daniel Dunbara49de6f2009-07-25 06:02:13 +000053Linker::warning(const StringRef &message) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000054 Error = message;
Dan Gohmanb4bc09b2008-10-25 17:57:20 +000055 if (!(Flags&QuietWarnings))
Daniel Dunbara49de6f2009-07-25 06:02:13 +000056 errs() << ProgramName << ": warning: " << message << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000057 return false;
58}
59
60void
Daniel Dunbara49de6f2009-07-25 06:02:13 +000061Linker::verbose(const StringRef &message) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000062 if (Flags&Verbose)
Daniel Dunbara49de6f2009-07-25 06:02:13 +000063 errs() << " " << message << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000064}
65
66void
67Linker::addPath(const sys::Path& path) {
68 LibPaths.push_back(path);
69}
70
71void
72Linker::addPaths(const std::vector<std::string>& paths) {
Chris Lattnerb1aa85b2009-08-23 22:45:37 +000073 for (unsigned i = 0, e = paths.size(); i != e; ++i)
74 LibPaths.push_back(sys::Path(paths[i]));
Dan Gohmanf17a25c2007-07-18 16:29:46 +000075}
76
77void
78Linker::addSystemPaths() {
79 sys::Path::GetBitcodeLibraryPaths(LibPaths);
80 LibPaths.insert(LibPaths.begin(),sys::Path("./"));
81}
82
83Module*
84Linker::releaseModule() {
85 Module* result = Composite;
86 LibPaths.clear();
87 Error.clear();
88 Composite = 0;
89 Flags = 0;
90 return result;
91}
92
93// LoadObject - Read in and parse the bitcode file named by FN and return the
94// module it contains (wrapped in an auto_ptr), or auto_ptr<Module>() and set
95// Error if an error occurs.
96std::auto_ptr<Module>
97Linker::LoadObject(const sys::Path &FN) {
98 std::string ParseErrorMessage;
99 Module *Result = 0;
100
Chris Lattnerb1aa85b2009-08-23 22:45:37 +0000101 std::auto_ptr<MemoryBuffer> Buffer(MemoryBuffer::getFileOrSTDIN(FN.c_str()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000102 if (Buffer.get())
Owen Anderson25209b42009-07-01 16:58:40 +0000103 Result = ParseBitcodeFile(Buffer.get(), Context, &ParseErrorMessage);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000104 else
Chris Lattnerb1aa85b2009-08-23 22:45:37 +0000105 ParseErrorMessage = "Error reading file '" + FN.str() + "'";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000106
107 if (Result)
108 return std::auto_ptr<Module>(Result);
Chris Lattnerb1aa85b2009-08-23 22:45:37 +0000109 Error = "Bitcode file '" + FN.str() + "' could not be loaded";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000110 if (ParseErrorMessage.size())
111 Error += ": " + ParseErrorMessage;
112 return std::auto_ptr<Module>();
113}
114
115// IsLibrary - Determine if "Name" is a library in "Directory". Return
116// a non-empty sys::Path if its found, an empty one otherwise.
Daniel Dunbara49de6f2009-07-25 06:02:13 +0000117static inline sys::Path IsLibrary(const StringRef &Name,
118 const sys::Path &Directory) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000119
120 sys::Path FullPath(Directory);
121
122 // Try the libX.a form
Daniel Dunbara49de6f2009-07-25 06:02:13 +0000123 FullPath.appendComponent(("lib" + Name).str());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000124 FullPath.appendSuffix("a");
125 if (FullPath.isArchive())
126 return FullPath;
127
128 // Try the libX.bca form
129 FullPath.eraseSuffix();
130 FullPath.appendSuffix("bca");
131 if (FullPath.isArchive())
132 return FullPath;
133
134 // 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;
139 if (FullPath.isBitcodeFile()) // .so file containing bitcode?
140 return FullPath;
141
142 // Not found .. fall through
143
144 // Indicate that the library was not found in the directory.
145 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
152/// library suffixes, in each of the directories in LibPaths. Returns an empty
153/// Path if no matching file can be found.
154///
155sys::Path
Daniel Dunbara49de6f2009-07-25 06:02:13 +0000156Linker::FindLib(const StringRef &Filename) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000157 // Determine if the pathname can be found as it stands.
158 sys::Path FilePath(Filename);
159 if (FilePath.canRead() &&
160 (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 Dunbara49de6f2009-07-25 06:02:13 +0000167 sys::Path FullPath = IsLibrary(Filename, Directory);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000168 if (!FullPath.isEmpty())
169 return FullPath;
170 }
171 return sys::Path();
172}