1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
|
//===- lib/Driver/Driver.cpp - Linker Driver Emulator ---------------------===//
//
// The LLVM Linker
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "lld/Core/ArchiveLibraryFile.h"
#include "lld/Core/File.h"
#include "lld/Core/Instrumentation.h"
#include "lld/Core/LLVM.h"
#include "lld/Core/Parallel.h"
#include "lld/Core/PassManager.h"
#include "lld/Core/Resolver.h"
#include "lld/Driver/Driver.h"
#include "lld/Driver/WrapperInputGraph.h"
#include "lld/Passes/RoundTripNativePass.h"
#include "lld/Passes/RoundTripYAMLPass.h"
#include "lld/ReaderWriter/Reader.h"
#include "lld/ReaderWriter/Writer.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/ADT/StringSwitch.h"
#include "llvm/Option/Arg.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/raw_ostream.h"
#include <mutex>
namespace lld {
FileVector makeErrorFile(StringRef path, std::error_code ec) {
std::vector<std::unique_ptr<File>> result;
result.push_back(llvm::make_unique<ErrorFile>(path, ec));
return result;
}
FileVector parseMemberFiles(FileVector &files) {
std::vector<std::unique_ptr<File>> members;
for (std::unique_ptr<File> &file : files) {
if (auto *archive = dyn_cast<ArchiveLibraryFile>(file.get())) {
if (std::error_code ec = archive->parseAllMembers(members))
return makeErrorFile(file->path(), ec);
} else {
members.push_back(std::move(file));
}
}
return members;
}
FileVector loadFile(LinkingContext &ctx, StringRef path, bool wholeArchive) {
ErrorOr<std::unique_ptr<MemoryBuffer>> mb
= MemoryBuffer::getFileOrSTDIN(path);
if (std::error_code ec = mb.getError())
return makeErrorFile(path, ec);
std::vector<std::unique_ptr<File>> files;
if (std::error_code ec = ctx.registry().loadFile(std::move(mb.get()), files))
return makeErrorFile(path, ec);
if (wholeArchive)
return parseMemberFiles(files);
return files;
}
/// This is where the link is actually performed.
bool Driver::link(LinkingContext &context, raw_ostream &diagnostics) {
// Honor -mllvm
if (!context.llvmOptions().empty()) {
unsigned numArgs = context.llvmOptions().size();
const char **args = new const char *[numArgs + 2];
args[0] = "lld (LLVM option parsing)";
for (unsigned i = 0; i != numArgs; ++i)
args[i + 1] = context.llvmOptions()[i];
args[numArgs + 1] = 0;
llvm::cl::ParseCommandLineOptions(numArgs + 1, args);
}
InputGraph &inputGraph = context.getInputGraph();
if (!inputGraph.size())
return false;
bool fail = false;
// Read inputs
ScopedTask readTask(getDefaultDomain(), "Read Args");
TaskGroup tg;
std::mutex diagnosticsMutex;
for (std::unique_ptr<InputElement> &ie : inputGraph.inputElements()) {
tg.spawn([&] {
// Writes to the same output stream is not guaranteed to be thread-safe.
// We buffer the diagnostics output to a separate string-backed output
// stream, acquire the lock, and then print it out.
std::string buf;
llvm::raw_string_ostream stream(buf);
if (std::error_code ec = ie->parse(context, stream)) {
if (FileNode *fileNode = dyn_cast<FileNode>(ie.get())) {
stream << "Cannot open " + fileNode->getFile()->path()
<< ": " << ec.message() << "\n";
} else {
llvm_unreachable("Unknown type of input element");
}
fail = true;
}
stream.flush();
if (!buf.empty()) {
std::lock_guard<std::mutex> lock(diagnosticsMutex);
diagnostics << buf;
}
});
}
tg.sync();
readTask.end();
if (fail)
return false;
InputGraph::FileVectorT internalFiles;
context.createInternalFiles(internalFiles);
for (auto i = internalFiles.rbegin(), e = internalFiles.rend(); i != e; ++i) {
context.getInputGraph().addInputElementFront(
llvm::make_unique<WrapperNode>(std::move(*i)));
}
// Give target a chance to add files.
InputGraph::FileVectorT implicitFiles;
context.createImplicitFiles(implicitFiles);
for (auto i = implicitFiles.rbegin(), e = implicitFiles.rend(); i != e; ++i) {
context.getInputGraph().addInputElementFront(
llvm::make_unique<WrapperNode>(std::move(*i)));
}
// Give target a chance to sort the input files.
// Mach-O uses this chance to move all object files before library files.
context.maybeSortInputFiles();
// Do core linking.
ScopedTask resolveTask(getDefaultDomain(), "Resolve");
Resolver resolver(context);
if (!resolver.resolve())
return false;
std::unique_ptr<MutableFile> merged = resolver.resultFile();
resolveTask.end();
// Run passes on linked atoms.
ScopedTask passTask(getDefaultDomain(), "Passes");
PassManager pm;
context.addPasses(pm);
#ifndef NDEBUG
if (context.runRoundTripPass()) {
pm.add(std::unique_ptr<Pass>(new RoundTripYAMLPass(context)));
pm.add(std::unique_ptr<Pass>(new RoundTripNativePass(context)));
}
#endif
pm.runOnFile(merged);
passTask.end();
// Give linked atoms to Writer to generate output file.
ScopedTask writeTask(getDefaultDomain(), "Write");
if (std::error_code ec = context.writeFile(*merged)) {
diagnostics << "Failed to write file '" << context.outputPath()
<< "': " << ec.message() << "\n";
return false;
}
return true;
}
} // namespace
|