Change allowsUnalignedMemoryAccesses to take type argument since some targets
[llvm/avr.git] / lib / Linker / Linker.cpp
blobbd569b5065394316ac366ae73bff31304e6d80ea
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/raw_ostream.h"
20 using namespace llvm;
22 Linker::Linker(const StringRef &progname, const StringRef &modname,
23 LLVMContext& C, unsigned flags):
24 Context(C),
25 Composite(new Module(modname, C)),
26 LibPaths(),
27 Flags(flags),
28 Error(),
29 ProgramName(progname) { }
31 Linker::Linker(const StringRef &progname, Module* aModule, unsigned flags) :
32 Context(aModule->getContext()),
33 Composite(aModule),
34 LibPaths(),
35 Flags(flags),
36 Error(),
37 ProgramName(progname) { }
39 Linker::~Linker() {
40 delete Composite;
43 bool
44 Linker::error(const StringRef &message) {
45 Error = message;
46 if (!(Flags&QuietErrors))
47 errs() << ProgramName << ": error: " << message << "\n";
48 return true;
51 bool
52 Linker::warning(const StringRef &message) {
53 Error = message;
54 if (!(Flags&QuietWarnings))
55 errs() << ProgramName << ": warning: " << message << "\n";
56 return false;
59 void
60 Linker::verbose(const StringRef &message) {
61 if (Flags&Verbose)
62 errs() << " " << message << "\n";
65 void
66 Linker::addPath(const sys::Path& path) {
67 LibPaths.push_back(path);
70 void
71 Linker::addPaths(const std::vector<std::string>& paths) {
72 for (unsigned i = 0; i != paths.size(); ++i) {
73 sys::Path aPath;
74 aPath.set(paths[i]);
75 LibPaths.push_back(aPath);
79 void
80 Linker::addSystemPaths() {
81 sys::Path::GetBitcodeLibraryPaths(LibPaths);
82 LibPaths.insert(LibPaths.begin(),sys::Path("./"));
85 Module*
86 Linker::releaseModule() {
87 Module* result = Composite;
88 LibPaths.clear();
89 Error.clear();
90 Composite = 0;
91 Flags = 0;
92 return result;
95 // LoadObject - Read in and parse the bitcode file named by FN and return the
96 // module it contains (wrapped in an auto_ptr), or auto_ptr<Module>() and set
97 // Error if an error occurs.
98 std::auto_ptr<Module>
99 Linker::LoadObject(const sys::Path &FN) {
100 std::string ParseErrorMessage;
101 Module *Result = 0;
103 const std::string &FNS = FN.toString();
104 std::auto_ptr<MemoryBuffer> Buffer(MemoryBuffer::getFileOrSTDIN(FNS.c_str()));
105 if (Buffer.get())
106 Result = ParseBitcodeFile(Buffer.get(), Context, &ParseErrorMessage);
107 else
108 ParseErrorMessage = "Error reading file '" + FNS + "'";
110 if (Result)
111 return std::auto_ptr<Module>(Result);
112 Error = "Bitcode file '" + FN.toString() + "' could not be loaded";
113 if (ParseErrorMessage.size())
114 Error += ": " + ParseErrorMessage;
115 return std::auto_ptr<Module>();
118 // IsLibrary - Determine if "Name" is a library in "Directory". Return
119 // a non-empty sys::Path if its found, an empty one otherwise.
120 static inline sys::Path IsLibrary(const StringRef &Name,
121 const sys::Path &Directory) {
123 sys::Path FullPath(Directory);
125 // Try the libX.a form
126 FullPath.appendComponent(("lib" + Name).str());
127 FullPath.appendSuffix("a");
128 if (FullPath.isArchive())
129 return FullPath;
131 // Try the libX.bca form
132 FullPath.eraseSuffix();
133 FullPath.appendSuffix("bca");
134 if (FullPath.isArchive())
135 return FullPath;
137 // Try the libX.so (or .dylib) form
138 FullPath.eraseSuffix();
139 FullPath.appendSuffix(&(LTDL_SHLIB_EXT[1]));
140 if (FullPath.isDynamicLibrary()) // Native shared library?
141 return FullPath;
142 if (FullPath.isBitcodeFile()) // .so file containing bitcode?
143 return FullPath;
145 // Not found .. fall through
147 // Indicate that the library was not found in the directory.
148 FullPath.clear();
149 return FullPath;
152 /// FindLib - Try to convert Filename into the name of a file that we can open,
153 /// if it does not already name a file we can open, by first trying to open
154 /// Filename, then libFilename.[suffix] for each of a set of several common
155 /// library suffixes, in each of the directories in LibPaths. Returns an empty
156 /// Path if no matching file can be found.
158 sys::Path
159 Linker::FindLib(const StringRef &Filename) {
160 // Determine if the pathname can be found as it stands.
161 sys::Path FilePath(Filename);
162 if (FilePath.canRead() &&
163 (FilePath.isArchive() || FilePath.isDynamicLibrary()))
164 return FilePath;
166 // Iterate over the directories in Paths to see if we can find the library
167 // there.
168 for (unsigned Index = 0; Index != LibPaths.size(); ++Index) {
169 sys::Path Directory(LibPaths[Index]);
170 sys::Path FullPath = IsLibrary(Filename, Directory);
171 if (!FullPath.isEmpty())
172 return FullPath;
174 return sys::Path();