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
|
//===- lib/Driver/InputGraph.cpp ------------------------------------------===//
//
// The LLVM Linker
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "lld/Core/Resolver.h"
#include "lld/Driver/InputGraph.h"
using namespace lld;
namespace {
bool sortInputElements(const std::unique_ptr<InputElement> &a,
const std::unique_ptr<InputElement> &b) {
return a->getOrdinal() < b->getOrdinal();
}
}
bool InputGraph::addInputElement(std::unique_ptr<InputElement> ie) {
_inputArgs.push_back(std::move(ie));
return true;
}
bool InputGraph::assignOrdinals() {
for (auto &ie : _inputArgs)
ie->setOrdinal(++_ordinal);
return true;
}
bool InputGraph::assignFileOrdinals(uint64_t &startOrdinal) {
for (auto &ie : _inputArgs)
ie->assignFileOrdinals(startOrdinal);
return true;
}
void InputGraph::doPostProcess() {
std::stable_sort(_inputArgs.begin(), _inputArgs.end(), sortInputElements);
}
bool InputGraph::validate() {
for (auto &ie : _inputArgs)
if (!ie->validate())
return false;
return true;
}
bool InputGraph::dump(raw_ostream &diagnostics) {
for (auto &ie : _inputArgs)
if (!ie->dump(diagnostics))
return false;
return true;
}
void InputGraph::insertElementsAt(
std::vector<std::unique_ptr<InputElement> > inputElements,
Position position, size_t pos) {
if (position == InputGraph::Position::BEGIN)
pos = 0;
else if (position == InputGraph::Position::END)
pos = _inputArgs.size();
_inputArgs.insert(_inputArgs.begin() + pos,
std::make_move_iterator(inputElements.begin()),
std::make_move_iterator(inputElements.end()));
}
void InputGraph::insertOneElementAt(std::unique_ptr<InputElement> element,
Position position, size_t pos) {
if (position == InputGraph::Position::BEGIN)
pos = 0;
else if (position == InputGraph::Position::END)
pos = _inputArgs.size();
_inputArgs.insert(_inputArgs.begin() + pos, std::move(element));
}
/// \brief Helper functions for the resolver
ErrorOr<InputElement *> InputGraph::getNextInputElement() {
if (_nextElementIndex >= _inputArgs.size())
return make_error_code(InputGraphError::no_more_elements);
return _inputArgs[_nextElementIndex++].get();
}
/// \brief Set the index on what inputElement has to be returned
ErrorOr<void> InputGraph::setNextElementIndex(uint32_t index) {
if (index > _inputArgs.size())
return make_error_code(llvm::errc::invalid_argument);
_nextElementIndex = index;
return error_code::success();
}
/// InputElement
/// \brief Initialize the Input Element, The ordinal value of an input Element
/// is initially set to -1, if the user wants to override its ordinal,
/// let the user do it
InputElement::InputElement(Kind type, int64_t ordinal)
: _kind(type), _ordinal(ordinal), _weight(0),
_resolveState(Resolver::StateNoChange), _nextFileIndex(0) {}
/// \brief Assign File ordinals for files contained
/// in the InputElement
void FileNode::assignFileOrdinals(uint64_t &startOrdinal) {
for (auto &file : _files)
file->setOrdinalAndIncrement(startOrdinal);
}
/// \brief Read the file into _buffer.
error_code
FileNode::readFile(const LinkingContext &ctx, raw_ostream &diagnostics) {
ErrorOr<StringRef> filePath = getPath(ctx);
if (!filePath &&
error_code(filePath) == llvm::errc::no_such_file_or_directory)
return make_error_code(llvm::errc::no_such_file_or_directory);
// Create a memory buffer
OwningPtr<llvm::MemoryBuffer> opmb;
if (error_code ec = llvm::MemoryBuffer::getFileOrSTDIN(*filePath, opmb))
return ec;
std::unique_ptr<MemoryBuffer> mb(opmb.take());
_buffer = std::move(mb);
if (ctx.logInputFiles())
diagnostics << _buffer->getBufferIdentifier() << "\n";
// YAML file is identified by a .objtxt extension
// FIXME : Identify YAML files by using a magic
if (filePath->endswith(".objtxt"))
if (error_code ec = ctx.getYAMLReader().parseFile(_buffer, _files))
return ec;
return error_code::success();
}
/// \brief Assign File ordinals for files contained
/// in the InputElement
void ControlNode::assignFileOrdinals(uint64_t &startOrdinal) {
for (auto &elem : _elements)
elem->assignFileOrdinals(startOrdinal);
}
/// \brief Assign File ordinals for files contained
/// in the InputElement
void SimpleFileNode::assignFileOrdinals(uint64_t &startOrdinal) {
for (auto &file : _files)
file->setOrdinalAndIncrement(startOrdinal);
}
|