blob: 6e0b760b85def5b0821bc4ebc02f7eae575aa220 [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"
17#include "llvm/Config/config.h"
18#include "llvm/Support/MemoryBuffer.h"
19#include "llvm/Support/Streams.h"
20using namespace llvm;
21
Chris Lattnerfc003612008-04-01 18:04:03 +000022Linker::Linker(const std::string& progname, const std::string& modname,
Owen Anderson92adb182009-07-01 23:13:44 +000023 LLVMContext& C, unsigned flags):
Owen Anderson25209b42009-07-01 16:58:40 +000024 Context(C),
25 Composite(new Module(modname, C)),
26 LibPaths(),
27 Flags(flags),
28 Error(),
29 ProgramName(progname) { }
Dan Gohmanf17a25c2007-07-18 16:29:46 +000030
Owen Anderson25209b42009-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) { }
Dan Gohmanf17a25c2007-07-18 16:29:46 +000038
39Linker::~Linker() {
40 delete Composite;
41}
42
43bool
44Linker::error(const std::string& message) {
45 Error = message;
46 if (!(Flags&QuietErrors))
47 cerr << ProgramName << ": error: " << message << "\n";
48 return true;
49}
50
51bool
52Linker::warning(const std::string& message) {
53 Error = message;
Dan Gohmanb4bc09b2008-10-25 17:57:20 +000054 if (!(Flags&QuietWarnings))
Dan Gohmanf17a25c2007-07-18 16:29:46 +000055 cerr << ProgramName << ": warning: " << message << "\n";
56 return false;
57}
58
59void
60Linker::verbose(const std::string& message) {
61 if (Flags&Verbose)
62 cerr << " " << message << "\n";
63}
64
65void
66Linker::addPath(const sys::Path& path) {
67 LibPaths.push_back(path);
68}
69
70void
71Linker::addPaths(const std::vector<std::string>& paths) {
72 for (unsigned i = 0; i != paths.size(); ++i) {
73 sys::Path aPath;
74 aPath.set(paths[i]);
75 LibPaths.push_back(aPath);
76 }
77}
78
79void
80Linker::addSystemPaths() {
81 sys::Path::GetBitcodeLibraryPaths(LibPaths);
82 LibPaths.insert(LibPaths.begin(),sys::Path("./"));
83}
84
85Module*
86Linker::releaseModule() {
87 Module* result = Composite;
88 LibPaths.clear();
89 Error.clear();
90 Composite = 0;
91 Flags = 0;
92 return result;
93}
94
95// LoadObject - Read in and parse the bitcode file named by FN and return the
96// module it contains (wrapped in an auto_ptr), or auto_ptr<Module>() and set
97// Error if an error occurs.
98std::auto_ptr<Module>
99Linker::LoadObject(const sys::Path &FN) {
100 std::string ParseErrorMessage;
101 Module *Result = 0;
102
103 const std::string &FNS = FN.toString();
Chris Lattnerfc003612008-04-01 18:04:03 +0000104 std::auto_ptr<MemoryBuffer> Buffer(MemoryBuffer::getFileOrSTDIN(FNS.c_str()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000105 if (Buffer.get())
Owen Anderson25209b42009-07-01 16:58:40 +0000106 Result = ParseBitcodeFile(Buffer.get(), Context, &ParseErrorMessage);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000107 else
108 ParseErrorMessage = "Error reading file '" + FNS + "'";
109
110 if (Result)
111 return std::auto_ptr<Module>(Result);
112 Error = "Bitcode file '" + FN.toString() + "' could not be loaded";
113 if (ParseErrorMessage.size())
114 Error += ": " + ParseErrorMessage;
115 return std::auto_ptr<Module>();
116}
117
118// IsLibrary - Determine if "Name" is a library in "Directory". Return
119// a non-empty sys::Path if its found, an empty one otherwise.
120static inline sys::Path IsLibrary(const std::string& Name,
121 const sys::Path& Directory) {
122
123 sys::Path FullPath(Directory);
124
125 // Try the libX.a form
126 FullPath.appendComponent("lib" + Name);
127 FullPath.appendSuffix("a");
128 if (FullPath.isArchive())
129 return FullPath;
130
131 // Try the libX.bca form
132 FullPath.eraseSuffix();
133 FullPath.appendSuffix("bca");
134 if (FullPath.isArchive())
135 return FullPath;
136
137 // 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;
142 if (FullPath.isBitcodeFile()) // .so file containing bitcode?
143 return FullPath;
144
145 // Not found .. fall through
146
147 // Indicate that the library was not found in the directory.
148 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
155/// library suffixes, in each of the directories in LibPaths. Returns an empty
156/// Path if no matching file can be found.
157///
158sys::Path
159Linker::FindLib(const std::string &Filename) {
160 // Determine if the pathname can be found as it stands.
161 sys::Path FilePath(Filename);
162 if (FilePath.canRead() &&
163 (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}