blob: ef242e5c798d585f47188297db3ff8d7737c6ff9 [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,
23 unsigned flags)
Reid Spencerde4cedc2004-12-13 03:00:28 +000024 : Composite(0)
25 , LibPaths()
26 , Flags(flags)
27 , Error()
28 , ProgramName(progname)
29{
Reid Spencer328ead92005-12-13 20:00:37 +000030 Composite = new Module(modname);
Reid Spencerde4cedc2004-12-13 03:00:28 +000031}
32
33Linker::Linker(const std::string& progname, Module* aModule, unsigned flags)
34 : Composite(aModule)
35 , LibPaths()
36 , Flags(flags)
37 , Error()
38 , ProgramName(progname)
39{
40}
41
42Linker::~Linker() {
43 delete Composite;
44}
45
Misha Brukmanf976c852005-04-21 22:55:34 +000046bool
Reid Spencerde4cedc2004-12-13 03:00:28 +000047Linker::error(const std::string& message) {
48 Error = message;
Bill Wendling41edad72006-11-27 10:09:12 +000049 if (!(Flags&QuietErrors))
Bill Wendlinge8156192006-12-07 01:30:32 +000050 cerr << ProgramName << ": error: " << message << "\n";
Reid Spencerde4cedc2004-12-13 03:00:28 +000051 return true;
52}
53
54bool
55Linker::warning(const std::string& message) {
56 Error = message;
Bill Wendling41edad72006-11-27 10:09:12 +000057 if (!(Flags&QuietErrors))
Bill Wendlinge8156192006-12-07 01:30:32 +000058 cerr << ProgramName << ": warning: " << message << "\n";
Reid Spencerde4cedc2004-12-13 03:00:28 +000059 return false;
60}
61
62void
63Linker::verbose(const std::string& message) {
Bill Wendling41edad72006-11-27 10:09:12 +000064 if (Flags&Verbose)
Bill Wendlinge8156192006-12-07 01:30:32 +000065 cerr << " " << message << "\n";
Reid Spencerde4cedc2004-12-13 03:00:28 +000066}
67
68void
69Linker::addPath(const sys::Path& path) {
Reid Spencerde4cedc2004-12-13 03:00:28 +000070 LibPaths.push_back(path);
71}
72
73void
74Linker::addPaths(const std::vector<std::string>& paths) {
Reid Spencer903f21d2004-12-13 03:50:50 +000075 for (unsigned i = 0; i != paths.size(); ++i) {
Reid Spencerde4cedc2004-12-13 03:00:28 +000076 sys::Path aPath;
Reid Spencerdd04df02005-07-07 23:21:43 +000077 aPath.set(paths[i]);
Reid Spencerde4cedc2004-12-13 03:00:28 +000078 LibPaths.push_back(aPath);
79 }
80}
81
82void
83Linker::addSystemPaths() {
Gabor Greifa99be512007-07-05 17:07:56 +000084 sys::Path::GetBitcodeLibraryPaths(LibPaths);
Reid Spencerde4cedc2004-12-13 03:00:28 +000085 LibPaths.insert(LibPaths.begin(),sys::Path("./"));
86}
87
88Module*
89Linker::releaseModule() {
90 Module* result = Composite;
Reid Spencerde4cedc2004-12-13 03:00:28 +000091 LibPaths.clear();
92 Error.clear();
Reid Spencer903f21d2004-12-13 03:50:50 +000093 Composite = 0;
Reid Spencerde4cedc2004-12-13 03:00:28 +000094 Flags = 0;
95 return result;
96}
97
Gabor Greifa99be512007-07-05 17:07:56 +000098// LoadObject - Read in and parse the bitcode file named by FN and return the
Misha Brukmanf976c852005-04-21 22:55:34 +000099// module it contains (wrapped in an auto_ptr), or auto_ptr<Module>() and set
Reid Spencerde4cedc2004-12-13 03:00:28 +0000100// Error if an error occurs.
Misha Brukmanf976c852005-04-21 22:55:34 +0000101std::auto_ptr<Module>
Reid Spencerde4cedc2004-12-13 03:00:28 +0000102Linker::LoadObject(const sys::Path &FN) {
103 std::string ParseErrorMessage;
Chris Lattner1a019e52007-05-06 06:02:13 +0000104 Module *Result = 0;
105
106 const std::string &FNS = FN.toString();
Chris Lattner038112a2008-04-01 18:04:03 +0000107 std::auto_ptr<MemoryBuffer> Buffer(MemoryBuffer::getFileOrSTDIN(FNS.c_str()));
Chris Lattner4bcca0f2007-05-06 09:29:13 +0000108 if (Buffer.get())
109 Result = ParseBitcodeFile(Buffer.get(), &ParseErrorMessage);
110 else
111 ParseErrorMessage = "Error reading file '" + FNS + "'";
Chris Lattner1a019e52007-05-06 06:02:13 +0000112
Misha Brukmanf976c852005-04-21 22:55:34 +0000113 if (Result)
Reid Spencerde4cedc2004-12-13 03:00:28 +0000114 return std::auto_ptr<Module>(Result);
Gabor Greifa99be512007-07-05 17:07:56 +0000115 Error = "Bitcode file '" + FN.toString() + "' could not be loaded";
Misha Brukmanf976c852005-04-21 22:55:34 +0000116 if (ParseErrorMessage.size())
Reid Spencerde4cedc2004-12-13 03:00:28 +0000117 Error += ": " + ParseErrorMessage;
118 return std::auto_ptr<Module>();
119}
120
Misha Brukmanf976c852005-04-21 22:55:34 +0000121// IsLibrary - Determine if "Name" is a library in "Directory". Return
Reid Spencer903f21d2004-12-13 03:50:50 +0000122// a non-empty sys::Path if its found, an empty one otherwise.
Misha Brukmanf976c852005-04-21 22:55:34 +0000123static inline sys::Path IsLibrary(const std::string& Name,
Reid Spencerde4cedc2004-12-13 03:00:28 +0000124 const sys::Path& Directory) {
125
Reid Spencerde4cedc2004-12-13 03:00:28 +0000126 sys::Path FullPath(Directory);
Reid Spencerde4cedc2004-12-13 03:00:28 +0000127
Chris Lattner501d5292006-07-28 22:52:11 +0000128 // Try the libX.a form
129 FullPath.appendComponent("lib" + Name);
130 FullPath.appendSuffix("a");
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.bca form
135 FullPath.eraseSuffix();
136 FullPath.appendSuffix("bca");
137 if (FullPath.isArchive())
138 return FullPath;
Reid Spencerde4cedc2004-12-13 03:00:28 +0000139
Chris Lattner501d5292006-07-28 22:52:11 +0000140 // Try the libX.so (or .dylib) form
141 FullPath.eraseSuffix();
142 FullPath.appendSuffix(&(LTDL_SHLIB_EXT[1]));
143 if (FullPath.isDynamicLibrary()) // Native shared library?
144 return FullPath;
Chris Lattner1a019e52007-05-06 06:02:13 +0000145 if (FullPath.isBitcodeFile()) // .so file containing bitcode?
146 return FullPath;
Reid Spencerde4cedc2004-12-13 03:00:28 +0000147
Chris Lattner501d5292006-07-28 22:52:11 +0000148 // Not found .. fall through
Reid Spencerdd04df02005-07-07 23:21:43 +0000149
150 // Indicate that the library was not found in the directory.
Reid Spencerde4cedc2004-12-13 03:00:28 +0000151 FullPath.clear();
152 return FullPath;
153}
154
155/// FindLib - Try to convert Filename into the name of a file that we can open,
156/// if it does not already name a file we can open, by first trying to open
157/// Filename, then libFilename.[suffix] for each of a set of several common
Misha Brukmanf976c852005-04-21 22:55:34 +0000158/// library suffixes, in each of the directories in LibPaths. Returns an empty
Reid Spencer903f21d2004-12-13 03:50:50 +0000159/// Path if no matching file can be found.
Reid Spencerde4cedc2004-12-13 03:00:28 +0000160///
Misha Brukmanf976c852005-04-21 22:55:34 +0000161sys::Path
John Criswell43da9c62006-01-17 22:01:57 +0000162Linker::FindLib(const std::string &Filename) {
Reid Spencerde4cedc2004-12-13 03:00:28 +0000163 // Determine if the pathname can be found as it stands.
164 sys::Path FilePath(Filename);
Reid Spencerc7f08322005-07-07 18:21:42 +0000165 if (FilePath.canRead() &&
Reid Spencerde4cedc2004-12-13 03:00:28 +0000166 (FilePath.isArchive() || FilePath.isDynamicLibrary()))
167 return FilePath;
168
169 // Iterate over the directories in Paths to see if we can find the library
170 // there.
171 for (unsigned Index = 0; Index != LibPaths.size(); ++Index) {
172 sys::Path Directory(LibPaths[Index]);
173 sys::Path FullPath = IsLibrary(Filename,Directory);
174 if (!FullPath.isEmpty())
175 return FullPath;
176 }
177 return sys::Path();
178}