Provide addc and subc
[llvm/msp430.git] / lib / Linker / Linker.cpp
blobd6737721d25fb74a5330e386737496a5cb8d3348
1 //===- lib/Linker/Linker.cpp - Basic Linker functionality ----------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
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/Bitcode/ReaderWriter.h"
17 #include "llvm/Config/config.h"
18 #include "llvm/Support/MemoryBuffer.h"
19 #include "llvm/Support/Streams.h"
20 using namespace llvm;
22 Linker::Linker(const std::string& progname, const std::string& modname,
23 unsigned flags)
24 : Composite(0)
25 , LibPaths()
26 , Flags(flags)
27 , Error()
28 , ProgramName(progname)
30 Composite = new Module(modname);
33 Linker::Linker(const std::string& progname, Module* aModule, unsigned flags)
34 : Composite(aModule)
35 , LibPaths()
36 , Flags(flags)
37 , Error()
38 , ProgramName(progname)
42 Linker::~Linker() {
43 delete Composite;
46 bool
47 Linker::error(const std::string& message) {
48 Error = message;
49 if (!(Flags&QuietErrors))
50 cerr << ProgramName << ": error: " << message << "\n";
51 return true;
54 bool
55 Linker::warning(const std::string& message) {
56 Error = message;
57 if (!(Flags&QuietWarnings))
58 cerr << ProgramName << ": warning: " << message << "\n";
59 return false;
62 void
63 Linker::verbose(const std::string& message) {
64 if (Flags&Verbose)
65 cerr << " " << message << "\n";
68 void
69 Linker::addPath(const sys::Path& path) {
70 LibPaths.push_back(path);
73 void
74 Linker::addPaths(const std::vector<std::string>& paths) {
75 for (unsigned i = 0; i != paths.size(); ++i) {
76 sys::Path aPath;
77 aPath.set(paths[i]);
78 LibPaths.push_back(aPath);
82 void
83 Linker::addSystemPaths() {
84 sys::Path::GetBitcodeLibraryPaths(LibPaths);
85 LibPaths.insert(LibPaths.begin(),sys::Path("./"));
88 Module*
89 Linker::releaseModule() {
90 Module* result = Composite;
91 LibPaths.clear();
92 Error.clear();
93 Composite = 0;
94 Flags = 0;
95 return result;
98 // LoadObject - Read in and parse the bitcode file named by FN and return the
99 // module it contains (wrapped in an auto_ptr), or auto_ptr<Module>() and set
100 // Error if an error occurs.
101 std::auto_ptr<Module>
102 Linker::LoadObject(const sys::Path &FN) {
103 std::string ParseErrorMessage;
104 Module *Result = 0;
106 const std::string &FNS = FN.toString();
107 std::auto_ptr<MemoryBuffer> Buffer(MemoryBuffer::getFileOrSTDIN(FNS.c_str()));
108 if (Buffer.get())
109 Result = ParseBitcodeFile(Buffer.get(), &ParseErrorMessage);
110 else
111 ParseErrorMessage = "Error reading file '" + FNS + "'";
113 if (Result)
114 return std::auto_ptr<Module>(Result);
115 Error = "Bitcode file '" + FN.toString() + "' could not be loaded";
116 if (ParseErrorMessage.size())
117 Error += ": " + ParseErrorMessage;
118 return std::auto_ptr<Module>();
121 // IsLibrary - Determine if "Name" is a library in "Directory". Return
122 // a non-empty sys::Path if its found, an empty one otherwise.
123 static inline sys::Path IsLibrary(const std::string& Name,
124 const sys::Path& Directory) {
126 sys::Path FullPath(Directory);
128 // Try the libX.a form
129 FullPath.appendComponent("lib" + Name);
130 FullPath.appendSuffix("a");
131 if (FullPath.isArchive())
132 return FullPath;
134 // Try the libX.bca form
135 FullPath.eraseSuffix();
136 FullPath.appendSuffix("bca");
137 if (FullPath.isArchive())
138 return FullPath;
140 // 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;
145 if (FullPath.isBitcodeFile()) // .so file containing bitcode?
146 return FullPath;
148 // Not found .. fall through
150 // Indicate that the library was not found in the directory.
151 FullPath.clear();
152 return FullPath;
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
158 /// library suffixes, in each of the directories in LibPaths. Returns an empty
159 /// Path if no matching file can be found.
161 sys::Path
162 Linker::FindLib(const std::string &Filename) {
163 // Determine if the pathname can be found as it stands.
164 sys::Path FilePath(Filename);
165 if (FilePath.canRead() &&
166 (FilePath.isArchive() || FilePath.isDynamicLibrary()))
167 return FilePath;
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;
177 return sys::Path();