blob: 64327e143ba771c8f7fbd0728a094fdf07a36544 [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
Reid Spencer328ead92005-12-13 20:00:37 +000022Linker::Linker(const std::string& progname, const std::string& modname, unsigned flags)
Reid Spencerde4cedc2004-12-13 03:00:28 +000023 : Composite(0)
24 , LibPaths()
25 , Flags(flags)
26 , Error()
27 , ProgramName(progname)
28{
Reid Spencer328ead92005-12-13 20:00:37 +000029 Composite = new Module(modname);
Reid Spencerde4cedc2004-12-13 03:00:28 +000030}
31
32Linker::Linker(const std::string& progname, Module* aModule, unsigned flags)
33 : Composite(aModule)
34 , LibPaths()
35 , Flags(flags)
36 , Error()
37 , ProgramName(progname)
38{
39}
40
41Linker::~Linker() {
42 delete Composite;
43}
44
Misha Brukmanf976c852005-04-21 22:55:34 +000045bool
Reid Spencerde4cedc2004-12-13 03:00:28 +000046Linker::error(const std::string& message) {
47 Error = message;
Bill Wendling41edad72006-11-27 10:09:12 +000048 if (!(Flags&QuietErrors))
Bill Wendlinge8156192006-12-07 01:30:32 +000049 cerr << ProgramName << ": error: " << message << "\n";
Reid Spencerde4cedc2004-12-13 03:00:28 +000050 return true;
51}
52
53bool
54Linker::warning(const std::string& message) {
55 Error = message;
Bill Wendling41edad72006-11-27 10:09:12 +000056 if (!(Flags&QuietErrors))
Bill Wendlinge8156192006-12-07 01:30:32 +000057 cerr << ProgramName << ": warning: " << message << "\n";
Reid Spencerde4cedc2004-12-13 03:00:28 +000058 return false;
59}
60
61void
62Linker::verbose(const std::string& message) {
Bill Wendling41edad72006-11-27 10:09:12 +000063 if (Flags&Verbose)
Bill Wendlinge8156192006-12-07 01:30:32 +000064 cerr << " " << 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) {
Reid Spencer903f21d2004-12-13 03:50:50 +000074 for (unsigned i = 0; i != paths.size(); ++i) {
Reid Spencerde4cedc2004-12-13 03:00:28 +000075 sys::Path aPath;
Reid Spencerdd04df02005-07-07 23:21:43 +000076 aPath.set(paths[i]);
Reid Spencerde4cedc2004-12-13 03:00:28 +000077 LibPaths.push_back(aPath);
78 }
79}
80
81void
82Linker::addSystemPaths() {
Gabor Greifa99be512007-07-05 17:07:56 +000083 sys::Path::GetBitcodeLibraryPaths(LibPaths);
Reid Spencerde4cedc2004-12-13 03:00:28 +000084 LibPaths.insert(LibPaths.begin(),sys::Path("./"));
85}
86
87Module*
88Linker::releaseModule() {
89 Module* result = Composite;
Reid Spencerde4cedc2004-12-13 03:00:28 +000090 LibPaths.clear();
91 Error.clear();
Reid Spencer903f21d2004-12-13 03:50:50 +000092 Composite = 0;
Reid Spencerde4cedc2004-12-13 03:00:28 +000093 Flags = 0;
94 return result;
95}
96
Gabor Greifa99be512007-07-05 17:07:56 +000097// LoadObject - Read in and parse the bitcode file named by FN and return the
Misha Brukmanf976c852005-04-21 22:55:34 +000098// module it contains (wrapped in an auto_ptr), or auto_ptr<Module>() and set
Reid Spencerde4cedc2004-12-13 03:00:28 +000099// Error if an error occurs.
Misha Brukmanf976c852005-04-21 22:55:34 +0000100std::auto_ptr<Module>
Reid Spencerde4cedc2004-12-13 03:00:28 +0000101Linker::LoadObject(const sys::Path &FN) {
102 std::string ParseErrorMessage;
Chris Lattner1a019e52007-05-06 06:02:13 +0000103 Module *Result = 0;
104
105 const std::string &FNS = FN.toString();
Chris Lattner4bcca0f2007-05-06 09:29:13 +0000106 std::auto_ptr<MemoryBuffer> Buffer(
Chris Lattner1a019e52007-05-06 06:02:13 +0000107 MemoryBuffer::getFileOrSTDIN(&FNS[0], FNS.size()));
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}