1 //===- lib/Linker/Linker.cpp - Basic Linker functionality ----------------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by Reid Spencer and is distributed under the
6 // University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains basic Linker functionality that all usages will need.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Linker.h"
15 #include "llvm/Module.h"
16 #include "llvm/Bytecode/Reader.h"
17 #include "llvm/Config/config.h"
22 Linker::Linker(const std::string
& progname
, const std::string
& modname
, unsigned flags
)
27 , ProgramName(progname
)
29 Composite
= new Module(modname
);
32 Linker::Linker(const std::string
& progname
, Module
* aModule
, unsigned flags
)
37 , ProgramName(progname
)
46 Linker::error(const std::string
& message
) {
48 if (!(Flags
&QuietErrors
)) {
49 std::cerr
<< ProgramName
<< ": error: " << message
<< "\n";
55 Linker::warning(const std::string
& message
) {
57 if (!(Flags
&QuietErrors
)) {
58 std::cerr
<< ProgramName
<< ": warning: " << message
<< "\n";
64 Linker::verbose(const std::string
& message
) {
66 std::cerr
<< " " << message
<< "\n";
71 Linker::addPath(const sys::Path
& path
) {
72 LibPaths
.push_back(path
);
76 Linker::addPaths(const std::vector
<std::string
>& paths
) {
77 for (unsigned i
= 0; i
!= paths
.size(); ++i
) {
80 LibPaths
.push_back(aPath
);
85 Linker::addSystemPaths() {
86 sys::Path::GetBytecodeLibraryPaths(LibPaths
);
87 LibPaths
.insert(LibPaths
.begin(),sys::Path("./"));
91 Linker::releaseModule() {
92 Module
* result
= Composite
;
100 // LoadObject - Read in and parse the bytecode file named by FN and return the
101 // module it contains (wrapped in an auto_ptr), or auto_ptr<Module>() and set
102 // Error if an error occurs.
103 std::auto_ptr
<Module
>
104 Linker::LoadObject(const sys::Path
&FN
) {
105 std::string ParseErrorMessage
;
106 Module
*Result
= ParseBytecodeFile(FN
.toString(), &ParseErrorMessage
);
108 return std::auto_ptr
<Module
>(Result
);
109 Error
= "Bytecode file '" + FN
.toString() + "' could not be loaded";
110 if (ParseErrorMessage
.size())
111 Error
+= ": " + ParseErrorMessage
;
112 return std::auto_ptr
<Module
>();
115 // IsLibrary - Determine if "Name" is a library in "Directory". Return
116 // a non-empty sys::Path if its found, an empty one otherwise.
117 static inline sys::Path
IsLibrary(const std::string
& Name
,
118 const sys::Path
& Directory
) {
120 sys::Path
FullPath(Directory
);
122 // Try the libX.a form
123 FullPath
.appendComponent("lib" + Name
);
124 FullPath
.appendSuffix("a");
125 if (FullPath
.isArchive())
128 // Try the libX.bca form
129 FullPath
.eraseSuffix();
130 FullPath
.appendSuffix("bca");
131 if (FullPath
.isArchive())
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?
139 if (FullPath
.isBytecodeFile()) // .so file containing bytecode?
142 // Not found .. fall through
144 // Indicate that the library was not found in the directory.
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.
156 Linker::FindLib(const std::string
&Filename
) {
157 // Determine if the pathname can be found as it stands.
158 sys::Path
FilePath(Filename
);
159 if (FilePath
.canRead() &&
160 (FilePath
.isArchive() || FilePath
.isDynamicLibrary()))
163 // Iterate over the directories in Paths to see if we can find the library
165 for (unsigned Index
= 0; Index
!= LibPaths
.size(); ++Index
) {
166 sys::Path
Directory(LibPaths
[Index
]);
167 sys::Path FullPath
= IsLibrary(Filename
,Directory
);
168 if (!FullPath
.isEmpty())