It's not legal to fold a load from a narrower stack slot into a wider instruction...
[llvm/avr.git] / lib / Linker / Linker.cpp
blobaef79d08f423ef3e373443b561b185dcc54aa378
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/System/Path.h"
18 #include "llvm/Support/MemoryBuffer.h"
19 #include "llvm/Support/raw_ostream.h"
20 #include "llvm/Config/config.h"
21 using namespace llvm;
23 Linker::Linker(const StringRef &progname, const StringRef &modname,
24 LLVMContext& C, unsigned flags):
25 Context(C),
26 Composite(new Module(modname, C)),
27 LibPaths(),
28 Flags(flags),
29 Error(),
30 ProgramName(progname) { }
32 Linker::Linker(const StringRef &progname, Module* aModule, unsigned flags) :
33 Context(aModule->getContext()),
34 Composite(aModule),
35 LibPaths(),
36 Flags(flags),
37 Error(),
38 ProgramName(progname) { }
40 Linker::~Linker() {
41 delete Composite;
44 bool
45 Linker::error(const StringRef &message) {
46 Error = message;
47 if (!(Flags&QuietErrors))
48 errs() << ProgramName << ": error: " << message << "\n";
49 return true;
52 bool
53 Linker::warning(const StringRef &message) {
54 Error = message;
55 if (!(Flags&QuietWarnings))
56 errs() << ProgramName << ": warning: " << message << "\n";
57 return false;
60 void
61 Linker::verbose(const StringRef &message) {
62 if (Flags&Verbose)
63 errs() << " " << message << "\n";
66 void
67 Linker::addPath(const sys::Path& path) {
68 LibPaths.push_back(path);
71 void
72 Linker::addPaths(const std::vector<std::string>& paths) {
73 for (unsigned i = 0, e = paths.size(); i != e; ++i)
74 LibPaths.push_back(sys::Path(paths[i]));
77 void
78 Linker::addSystemPaths() {
79 sys::Path::GetBitcodeLibraryPaths(LibPaths);
80 LibPaths.insert(LibPaths.begin(),sys::Path("./"));
83 Module*
84 Linker::releaseModule() {
85 Module* result = Composite;
86 LibPaths.clear();
87 Error.clear();
88 Composite = 0;
89 Flags = 0;
90 return result;
93 // LoadObject - Read in and parse the bitcode file named by FN and return the
94 // module it contains (wrapped in an auto_ptr), or auto_ptr<Module>() and set
95 // Error if an error occurs.
96 std::auto_ptr<Module>
97 Linker::LoadObject(const sys::Path &FN) {
98 std::string ParseErrorMessage;
99 Module *Result = 0;
101 std::auto_ptr<MemoryBuffer> Buffer(MemoryBuffer::getFileOrSTDIN(FN.c_str()));
102 if (Buffer.get())
103 Result = ParseBitcodeFile(Buffer.get(), Context, &ParseErrorMessage);
104 else
105 ParseErrorMessage = "Error reading file '" + FN.str() + "'";
107 if (Result)
108 return std::auto_ptr<Module>(Result);
109 Error = "Bitcode file '" + FN.str() + "' 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 StringRef &Name,
118 const sys::Path &Directory) {
120 sys::Path FullPath(Directory);
122 // Try the libX.a form
123 FullPath.appendComponent(("lib" + Name).str());
124 FullPath.appendSuffix("a");
125 if (FullPath.isArchive())
126 return FullPath;
128 // Try the libX.bca form
129 FullPath.eraseSuffix();
130 FullPath.appendSuffix("bca");
131 if (FullPath.isArchive())
132 return FullPath;
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?
138 return FullPath;
139 if (FullPath.isBitcodeFile()) // .so file containing bitcode?
140 return FullPath;
142 // Not found .. fall through
144 // Indicate that the library was not found in the directory.
145 FullPath.clear();
146 return FullPath;
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.
155 sys::Path
156 Linker::FindLib(const StringRef &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()))
161 return FilePath;
163 // Iterate over the directories in Paths to see if we can find the library
164 // there.
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())
169 return FullPath;
171 return sys::Path();