blob: bf331960905bae1a2b42347b92f0595d94c869fc [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//
Misha Brukmanf976c852005-04-21 22:55:34 +00005// This file was developed by Reid Spencer and is distributed under the
Reid Spencerde4cedc2004-12-13 03:00:28 +00006// University of Illinois Open Source 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"
16#include "llvm/Bytecode/Reader.h"
Reid Spencera834f5d2004-12-16 19:19:24 +000017#include "llvm/Config/config.h"
Bill Wendling41edad72006-11-27 10:09:12 +000018#include "llvm/Support/Streams.h"
Reid Spencerde4cedc2004-12-13 03:00:28 +000019using namespace llvm;
20
Reid Spencer328ead92005-12-13 20:00:37 +000021Linker::Linker(const std::string& progname, const std::string& modname, unsigned flags)
Reid Spencerde4cedc2004-12-13 03:00:28 +000022 : Composite(0)
23 , LibPaths()
24 , Flags(flags)
25 , Error()
26 , ProgramName(progname)
27{
Reid Spencer328ead92005-12-13 20:00:37 +000028 Composite = new Module(modname);
Reid Spencerde4cedc2004-12-13 03:00:28 +000029}
30
31Linker::Linker(const std::string& progname, Module* aModule, unsigned flags)
32 : Composite(aModule)
33 , LibPaths()
34 , Flags(flags)
35 , Error()
36 , ProgramName(progname)
37{
38}
39
40Linker::~Linker() {
41 delete Composite;
42}
43
Misha Brukmanf976c852005-04-21 22:55:34 +000044bool
Reid Spencerde4cedc2004-12-13 03:00:28 +000045Linker::error(const std::string& message) {
46 Error = message;
Bill Wendling41edad72006-11-27 10:09:12 +000047 if (!(Flags&QuietErrors))
48 llvm_cerr << ProgramName << ": error: " << message << "\n";
Reid Spencerde4cedc2004-12-13 03:00:28 +000049 return true;
50}
51
52bool
53Linker::warning(const std::string& message) {
54 Error = message;
Bill Wendling41edad72006-11-27 10:09:12 +000055 if (!(Flags&QuietErrors))
56 llvm_cerr << ProgramName << ": warning: " << message << "\n";
Reid Spencerde4cedc2004-12-13 03:00:28 +000057 return false;
58}
59
60void
61Linker::verbose(const std::string& message) {
Bill Wendling41edad72006-11-27 10:09:12 +000062 if (Flags&Verbose)
63 llvm_cerr << " " << message << "\n";
Reid Spencerde4cedc2004-12-13 03:00:28 +000064}
65
66void
67Linker::addPath(const sys::Path& path) {
Reid Spencerde4cedc2004-12-13 03:00:28 +000068 LibPaths.push_back(path);
69}
70
71void
72Linker::addPaths(const std::vector<std::string>& paths) {
Reid Spencer903f21d2004-12-13 03:50:50 +000073 for (unsigned i = 0; i != paths.size(); ++i) {
Reid Spencerde4cedc2004-12-13 03:00:28 +000074 sys::Path aPath;
Reid Spencerdd04df02005-07-07 23:21:43 +000075 aPath.set(paths[i]);
Reid Spencerde4cedc2004-12-13 03:00:28 +000076 LibPaths.push_back(aPath);
77 }
78}
79
80void
81Linker::addSystemPaths() {
82 sys::Path::GetBytecodeLibraryPaths(LibPaths);
83 LibPaths.insert(LibPaths.begin(),sys::Path("./"));
84}
85
86Module*
87Linker::releaseModule() {
88 Module* result = Composite;
Reid Spencerde4cedc2004-12-13 03:00:28 +000089 LibPaths.clear();
90 Error.clear();
Reid Spencer903f21d2004-12-13 03:50:50 +000091 Composite = 0;
Reid Spencerde4cedc2004-12-13 03:00:28 +000092 Flags = 0;
93 return result;
94}
95
96// LoadObject - Read in and parse the bytecode file named by FN and return the
Misha Brukmanf976c852005-04-21 22:55:34 +000097// module it contains (wrapped in an auto_ptr), or auto_ptr<Module>() and set
Reid Spencerde4cedc2004-12-13 03:00:28 +000098// Error if an error occurs.
Misha Brukmanf976c852005-04-21 22:55:34 +000099std::auto_ptr<Module>
Reid Spencerde4cedc2004-12-13 03:00:28 +0000100Linker::LoadObject(const sys::Path &FN) {
101 std::string ParseErrorMessage;
102 Module *Result = ParseBytecodeFile(FN.toString(), &ParseErrorMessage);
Misha Brukmanf976c852005-04-21 22:55:34 +0000103 if (Result)
Reid Spencerde4cedc2004-12-13 03:00:28 +0000104 return std::auto_ptr<Module>(Result);
105 Error = "Bytecode file '" + FN.toString() + "' could not be loaded";
Misha Brukmanf976c852005-04-21 22:55:34 +0000106 if (ParseErrorMessage.size())
Reid Spencerde4cedc2004-12-13 03:00:28 +0000107 Error += ": " + ParseErrorMessage;
108 return std::auto_ptr<Module>();
109}
110
Misha Brukmanf976c852005-04-21 22:55:34 +0000111// IsLibrary - Determine if "Name" is a library in "Directory". Return
Reid Spencer903f21d2004-12-13 03:50:50 +0000112// a non-empty sys::Path if its found, an empty one otherwise.
Misha Brukmanf976c852005-04-21 22:55:34 +0000113static inline sys::Path IsLibrary(const std::string& Name,
Reid Spencerde4cedc2004-12-13 03:00:28 +0000114 const sys::Path& Directory) {
115
Reid Spencerde4cedc2004-12-13 03:00:28 +0000116 sys::Path FullPath(Directory);
Reid Spencerde4cedc2004-12-13 03:00:28 +0000117
Chris Lattner501d5292006-07-28 22:52:11 +0000118 // Try the libX.a form
119 FullPath.appendComponent("lib" + Name);
120 FullPath.appendSuffix("a");
121 if (FullPath.isArchive())
122 return FullPath;
Reid Spencerde4cedc2004-12-13 03:00:28 +0000123
Chris Lattner501d5292006-07-28 22:52:11 +0000124 // Try the libX.bca form
125 FullPath.eraseSuffix();
126 FullPath.appendSuffix("bca");
127 if (FullPath.isArchive())
128 return FullPath;
Reid Spencerde4cedc2004-12-13 03:00:28 +0000129
Chris Lattner501d5292006-07-28 22:52:11 +0000130 // Try the libX.so (or .dylib) form
131 FullPath.eraseSuffix();
132 FullPath.appendSuffix(&(LTDL_SHLIB_EXT[1]));
133 if (FullPath.isDynamicLibrary()) // Native shared library?
134 return FullPath;
135 if (FullPath.isBytecodeFile()) // .so file containing bytecode?
136 return FullPath;
Reid Spencerde4cedc2004-12-13 03:00:28 +0000137
Chris Lattner501d5292006-07-28 22:52:11 +0000138 // Not found .. fall through
Reid Spencerdd04df02005-07-07 23:21:43 +0000139
140 // Indicate that the library was not found in the directory.
Reid Spencerde4cedc2004-12-13 03:00:28 +0000141 FullPath.clear();
142 return FullPath;
143}
144
145/// FindLib - Try to convert Filename into the name of a file that we can open,
146/// if it does not already name a file we can open, by first trying to open
147/// Filename, then libFilename.[suffix] for each of a set of several common
Misha Brukmanf976c852005-04-21 22:55:34 +0000148/// library suffixes, in each of the directories in LibPaths. Returns an empty
Reid Spencer903f21d2004-12-13 03:50:50 +0000149/// Path if no matching file can be found.
Reid Spencerde4cedc2004-12-13 03:00:28 +0000150///
Misha Brukmanf976c852005-04-21 22:55:34 +0000151sys::Path
John Criswell43da9c62006-01-17 22:01:57 +0000152Linker::FindLib(const std::string &Filename) {
Reid Spencerde4cedc2004-12-13 03:00:28 +0000153 // Determine if the pathname can be found as it stands.
154 sys::Path FilePath(Filename);
Reid Spencerc7f08322005-07-07 18:21:42 +0000155 if (FilePath.canRead() &&
Reid Spencerde4cedc2004-12-13 03:00:28 +0000156 (FilePath.isArchive() || FilePath.isDynamicLibrary()))
157 return FilePath;
158
159 // Iterate over the directories in Paths to see if we can find the library
160 // there.
161 for (unsigned Index = 0; Index != LibPaths.size(); ++Index) {
162 sys::Path Directory(LibPaths[Index]);
163 sys::Path FullPath = IsLibrary(Filename,Directory);
164 if (!FullPath.isEmpty())
165 return FullPath;
166 }
167 return sys::Path();
168}