blob: c07bf56a34515fd826f61475282de697da9b8173 [file] [log] [blame]
Nick Kledzik6b079f52013-01-05 02:22:35 +00001//===- lib/ReaderWriter/YAML/ReaderWriterYAML.cpp -------------------------===//
2//
3// The LLVM Linker
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10
Michael J. Spencer64afcb42013-01-23 01:18:43 +000011#include "lld/ReaderWriter/Reader.h"
12#include "lld/ReaderWriter/Writer.h"
13
Nick Kledzik6b079f52013-01-05 02:22:35 +000014#include "lld/Core/ArchiveLibraryFile.h"
15#include "lld/Core/DefinedAtom.h"
16#include "lld/Core/Error.h"
17#include "lld/Core/File.h"
18#include "lld/Core/LLVM.h"
19#include "lld/Core/Reference.h"
Nick Kledzik6b079f52013-01-05 02:22:35 +000020
21#include "llvm/ADT/ArrayRef.h"
22#include "llvm/ADT/OwningPtr.h"
23#include "llvm/ADT/StringMap.h"
24#include "llvm/ADT/Twine.h"
25#include "llvm/Support/Debug.h"
26#include "llvm/Support/ErrorHandling.h"
27#include "llvm/Support/Format.h"
28#include "llvm/Support/MemoryBuffer.h"
29#include "llvm/Support/raw_ostream.h"
30#include "llvm/Support/system_error.h"
31#include "llvm/Support/YAMLTraits.h"
32
33#include <string>
34
35using llvm::yaml::MappingTraits;
36using llvm::yaml::ScalarEnumerationTraits;
37using llvm::yaml::ScalarTraits;
38using llvm::yaml::IO;
39using llvm::yaml::SequenceTraits;
40using llvm::yaml::DocumentListTraits;
41
Michael J. Spencer64afcb42013-01-23 01:18:43 +000042using namespace lld;
Nick Kledzik6b079f52013-01-05 02:22:35 +000043
44/// The conversion of Atoms to and from YAML uses LLVM's YAML I/O. This
45/// file just defines template specializations on the lld types which control
46/// how the mapping is done to and from YAML.
47
48
49namespace {
50/// Most of the traits are context-free and always do the same transformation.
51/// But, there are some traits that need some contextual information to properly
Shankar Easwaran8962feb2013-03-14 16:09:49 +000052/// do their transform. This struct is available via io.getContext() and
Nick Kledzik6b079f52013-01-05 02:22:35 +000053/// supplies contextual information.
54class ContextInfo {
55public:
Michael J. Spencer64afcb42013-01-23 01:18:43 +000056 ContextInfo(const TargetInfo &ti) : _currentFile(nullptr), _targetInfo(ti) {}
Nick Kledzik6b079f52013-01-05 02:22:35 +000057
Michael J. Spencer64afcb42013-01-23 01:18:43 +000058 lld::File *_currentFile;
59 const TargetInfo &_targetInfo;
Nick Kledzik6b079f52013-01-05 02:22:35 +000060};
61
Nick Kledzik6b079f52013-01-05 02:22:35 +000062/// Used when writing yaml files.
63/// In most cases, atoms names are unambiguous, so references can just
64/// use the atom name as the target (e.g. target: foo). But in a few
65/// cases that does not work, so ref-names are added. These are labels
66/// used only in yaml. The labels do not exist in the Atom model.
67///
68/// One need for ref-names are when atoms have no user supplied name
69/// (e.g. c-string literal). Another case is when two object files with
70/// identically named static functions are merged (ld -r) into one object file.
71/// In that case referencing the function by name is ambiguous, so a unique
72/// ref-name is added.
73class RefNameBuilder {
74public:
Shankar Easwaran8962feb2013-03-14 16:09:49 +000075 RefNameBuilder(const lld::File &file)
Nick Kledzik6b079f52013-01-05 02:22:35 +000076 : _collisionCount(0), _unnamedCounter(0) {
77 if (&file == nullptr)
78 return;
79 // visit all atoms
80 for (const lld::DefinedAtom *atom : file.defined()) {
81 // Build map of atoms names to detect duplicates
82 if (!atom->name().empty())
83 buildDuplicateNameMap(*atom);
84
85 // Find references to unnamed atoms and create ref-names for them.
86 for (const lld::Reference *ref : *atom) {
87 // create refname for any unnamed reference target
88 const lld::Atom *target = ref->target();
89 if ((target != nullptr) && target->name().empty()) {
90 std::string storage;
91 llvm::raw_string_ostream buffer(storage);
92 buffer << llvm::format("L%03d", _unnamedCounter++);
93 llvm::StringRef newName = copyString(buffer.str());
94 _refNames[target] = newName;
Shankar Easwaran8962feb2013-03-14 16:09:49 +000095 DEBUG_WITH_TYPE("WriterYAML", llvm::dbgs()
96 << "unnamed atom: creating ref-name: '" << newName
97 << "' (" << (void*)newName.data() << ", "
Nick Kledzik6b079f52013-01-05 02:22:35 +000098 << newName.size() << ")\n");
99 }
100 }
101 }
102 for (const lld::UndefinedAtom *undefAtom : file.undefined()) {
103 buildDuplicateNameMap(*undefAtom);
104 }
105 for (const lld::SharedLibraryAtom *shlibAtom : file.sharedLibrary()) {
106 buildDuplicateNameMap(*shlibAtom);
107 }
108 for (const lld::AbsoluteAtom *absAtom : file.absolute()) {
109 buildDuplicateNameMap(*absAtom);
110 }
111 }
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000112
Nick Kledzik6b079f52013-01-05 02:22:35 +0000113 void buildDuplicateNameMap(const lld::Atom &atom) {
114 assert(!atom.name().empty());
115 NameToAtom::iterator pos = _nameMap.find(atom.name());
116 if ( pos != _nameMap.end() ) {
117 // Found name collision, give each a unique ref-name.
118 std::string Storage;
119 llvm::raw_string_ostream buffer(Storage);
120 buffer << atom.name() << llvm::format(".%03d", ++_collisionCount);
121 llvm::StringRef newName = copyString(buffer.str());
122 _refNames[&atom] = newName;
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000123 DEBUG_WITH_TYPE("WriterYAML", llvm::dbgs()
Nick Kledzik6b079f52013-01-05 02:22:35 +0000124 << "name collsion: creating ref-name: '" << newName
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000125 << "' (" << (void*)newName.data() << ", "
Nick Kledzik6b079f52013-01-05 02:22:35 +0000126 << newName.size() << ")\n");
127 const lld::Atom *prevAtom = pos->second;
128 AtomToRefName::iterator pos2 = _refNames.find(prevAtom);
129 if ( pos2 == _refNames.end() ) {
130 // Only create ref-name for previous if none already created.
131 std::string Storage2;
132 llvm::raw_string_ostream buffer2(Storage2);
133 buffer2 << prevAtom->name() << llvm::format(".%03d", ++_collisionCount);
134 llvm::StringRef newName2 = copyString(buffer2.str());
135 _refNames[prevAtom] = newName2;
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000136 DEBUG_WITH_TYPE("WriterYAML", llvm::dbgs()
Nick Kledzik6b079f52013-01-05 02:22:35 +0000137 << "name collsion: creating ref-name: '" << newName2
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000138 << "' (" << (void*)newName2.data() << ", "
Nick Kledzik6b079f52013-01-05 02:22:35 +0000139 << newName2.size() << ")\n");
140 }
141 }
142 else {
143 // First time we've seen this name, just add it to map.
144 _nameMap[atom.name()] = &atom;
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000145 DEBUG_WITH_TYPE("WriterYAML", llvm::dbgs()
146 << "atom name seen for first time: '" << atom.name()
147 << "' (" << (void*)atom.name().data() << ", "
Nick Kledzik6b079f52013-01-05 02:22:35 +0000148 << atom.name().size() << ")\n");
149 }
150 }
151
152 bool hasRefName(const lld::Atom *atom) {
153 return _refNames.count(atom);
154 }
155
156 llvm::StringRef refName(const lld::Atom *atom) {
157 return _refNames.find(atom)->second;
158 }
159
160private:
161 typedef llvm::StringMap<const lld::Atom*> NameToAtom;
162 typedef llvm::DenseMap<const lld::Atom*, std::string> AtomToRefName;
163
164 // Allocate a new copy of this string and keep track of allocations
165 // in _stringCopies, so they can be freed when RefNameBuilder is destroyed.
166 llvm::StringRef copyString(llvm::StringRef str) {
167 // We want _stringCopies to own the string memory so it is deallocated
168 // when the File object is destroyed. But we need a StringRef that
169 // points into that memory.
Michael J. Spencer20231f12013-01-26 12:26:56 +0000170 std::unique_ptr<char[]> s(new char[str.size()]);
Nick Kledzik6b079f52013-01-05 02:22:35 +0000171 memcpy(s.get(), str.data(), str.size());
172 llvm::StringRef r = llvm::StringRef(s.get(), str.size());
173 _stringCopies.push_back(std::move(s));
174 return r;
175 }
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000176
Nick Kledzik6b079f52013-01-05 02:22:35 +0000177 unsigned int _collisionCount;
178 unsigned int _unnamedCounter;
179 NameToAtom _nameMap;
180 AtomToRefName _refNames;
Michael J. Spencer20231f12013-01-26 12:26:56 +0000181 std::vector<std::unique_ptr<char[]>> _stringCopies;
Nick Kledzik6b079f52013-01-05 02:22:35 +0000182};
183
184
185/// Used when reading yaml files to find the target of a reference
186/// that could be a name or ref-name.
187class RefNameResolver {
188public:
189 RefNameResolver(const lld::File *file, IO &io);
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000190
Nick Kledzik6b079f52013-01-05 02:22:35 +0000191 const lld::Atom *lookup(llvm::StringRef name) const {
192 NameToAtom::const_iterator pos = _nameMap.find(name);
193 if (pos != _nameMap.end()) {
194 return pos->second;
195 }
196 else {
197 _io.setError(llvm::Twine("no such atom name: ") + name);
198 return nullptr;
199 }
200 }
201
202private:
203 typedef llvm::StringMap<const lld::Atom*> NameToAtom;
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000204
Nick Kledzik6b079f52013-01-05 02:22:35 +0000205 void add(llvm::StringRef name, const lld::Atom *atom) {
206 if (_nameMap.count(name)) {
207 _io.setError(llvm::Twine("duplicate atom name: ") + name);
208 }
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000209 else {
Nick Kledzik6b079f52013-01-05 02:22:35 +0000210 _nameMap[name] = atom;
211 }
212 }
213
214 IO &_io;
215 NameToAtom _nameMap;
216};
217
218
219// Used in NormalizedFile to hold the atoms lists.
220template <typename T>
221class AtomList : public lld::File::atom_collection<T> {
222public:
Michael J. Spencerc3c8bc12013-02-11 23:03:35 +0000223 virtual lld::File::atom_iterator<T> begin() const {
224 return lld::File::atom_iterator<
225 T>(*this,
226 _atoms.empty() ? 0 : reinterpret_cast<const void *>(_atoms.data()));
Nick Kledzik6b079f52013-01-05 02:22:35 +0000227 }
Michael J. Spencerc3c8bc12013-02-11 23:03:35 +0000228 virtual lld::File::atom_iterator<T> end() const{
229 return lld::File::atom_iterator<
230 T>(*this, _atoms.empty() ? 0 :
231 reinterpret_cast<const void *>(_atoms.data() + _atoms.size()));
Nick Kledzik6b079f52013-01-05 02:22:35 +0000232 }
233 virtual const T *deref(const void *it) const {
234 return *reinterpret_cast<const T *const*>(it);
235 }
236 virtual void next(const void *&it) const {
237 const T *const *p = reinterpret_cast<const T *const *>(it);
238 ++p;
239 it = reinterpret_cast<const void*>(p);
240 }
241 virtual void push_back(const T *element) {
242 _atoms.push_back(element);
243 }
244 std::vector<const T*> _atoms;
245};
246
247/// Mapping of kind: field in yaml files.
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000248enum FileKinds {
Nick Kledzik6b079f52013-01-05 02:22:35 +0000249 fileKindObjectAtoms, // atom based object file encoded in yaml
250 fileKindArchive, // static archive library encoded in yaml
251 fileKindObjectELF, // ELF object files encoded in yaml
252 fileKindObjectMachO // mach-o object files encoded in yaml
253};
254
255struct ArchMember {
256 FileKinds _kind;
257 llvm::StringRef _name;
258 const lld::File *_content;
259};
260
Nick Kledzikbd491982013-01-08 23:51:03 +0000261
Nick Kledzik6b079f52013-01-05 02:22:35 +0000262// The content bytes in a DefinedAtom are just uint8_t but we want
263// special formatting, so define a strong type.
Nick Kledzikbd491982013-01-08 23:51:03 +0000264LLVM_YAML_STRONG_TYPEDEF(uint8_t, ImplicitHex8)
Nick Kledzik6b079f52013-01-05 02:22:35 +0000265
266// SharedLibraryAtoms have a bool canBeNull() method which we'd like to be
267// more readable than just true/false.
Nick Kledzikbd491982013-01-08 23:51:03 +0000268LLVM_YAML_STRONG_TYPEDEF(bool, ShlibCanBeNull)
Nick Kledzik6b079f52013-01-05 02:22:35 +0000269
270// lld::Reference::Kind is a typedef of int32. We need a stronger
271// type to make template matching work, so invent RefKind.
Nick Kledzikbd491982013-01-08 23:51:03 +0000272LLVM_YAML_STRONG_TYPEDEF(lld::Reference::Kind, RefKind)
Nick Kledzik6b079f52013-01-05 02:22:35 +0000273
Nick Kledzik6b079f52013-01-05 02:22:35 +0000274} // namespace anon
275
Shankar Easwaran3256d4f2013-01-25 07:39:18 +0000276LLVM_YAML_IS_SEQUENCE_VECTOR(ArchMember)
277 LLVM_YAML_IS_SEQUENCE_VECTOR(const lld::Reference *)
278 // Always write DefinedAtoms content bytes as a flow sequence.
279 LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(ImplicitHex8)
280 // for compatibility with gcc-4.7 in C++11 mode, add extra namespace
281 namespace llvm {
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000282 namespace yaml {
Nick Kledzikbd491982013-01-08 23:51:03 +0000283
Nick Kledzik6b079f52013-01-05 02:22:35 +0000284// This is a custom formatter for RefKind
285template<>
286struct ScalarTraits<RefKind> {
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000287 static void output(const RefKind &value, void *ctxt,
Nick Kledzik6b079f52013-01-05 02:22:35 +0000288 llvm::raw_ostream &out) {
289 assert(ctxt != nullptr);
290 ContextInfo *info = reinterpret_cast<ContextInfo*>(ctxt);
Nick Kledzikc314b462013-04-04 18:59:24 +0000291 switch (value) {
292 case lld::Reference::kindLayoutAfter:
293 out << "layout-after";
294 break;
295 case lld::Reference::kindLayoutBefore:
296 out << "layout-before";
297 break;
298 case lld::Reference::kindInGroup:
299 out << "in-group";
300 break;
301 default:
302 if (auto relocStr = info->_targetInfo.stringFromRelocKind(value))
303 out << *relocStr;
304 else
305 out << "<unknown>";
306 break;
307 }
308 }
Nick Kledzik6b079f52013-01-05 02:22:35 +0000309
310 static StringRef input(StringRef scalar, void *ctxt, RefKind &value) {
311 assert(ctxt != nullptr);
312 ContextInfo *info = reinterpret_cast<ContextInfo*>(ctxt);
Michael J. Spencer64afcb42013-01-23 01:18:43 +0000313 auto relocKind = info->_targetInfo.relocKindFromString(scalar);
Nick Kledzikc314b462013-04-04 18:59:24 +0000314 if (!relocKind) {
315 if (scalar.equals("layout-after")) {
316 value = lld::Reference::kindLayoutAfter;
317 return StringRef();
318 }
319 if (scalar.equals("layout-before")) {
320 value = lld::Reference::kindLayoutBefore;
321 return StringRef();
322 }
323 if (scalar.equals("in-group")) {
324 value = lld::Reference::kindInGroup;
325 return StringRef();
326 }
Michael J. Spencer64afcb42013-01-23 01:18:43 +0000327 return "Invalid relocation kind";
Nick Kledzikc314b462013-04-04 18:59:24 +0000328 }
Michael J. Spencer64afcb42013-01-23 01:18:43 +0000329 value = *relocKind;
Nick Kledzik6b079f52013-01-05 02:22:35 +0000330 return StringRef();
331 }
332};
333
334
335template <>
336struct ScalarEnumerationTraits<lld::File::Kind> {
337 static void enumeration(IO &io, lld::File::Kind &value) {
338 io.enumCase(value, "object", lld::File::kindObject);
339 io.enumCase(value, "shared-library", lld::File::kindSharedLibrary);
340 io.enumCase(value, "static-library", lld::File::kindArchiveLibrary);
341 }
342};
343
344template <>
345struct ScalarEnumerationTraits<lld::Atom::Scope> {
346 static void enumeration(IO &io, lld::Atom::Scope &value) {
347 io.enumCase(value, "global", lld::Atom::scopeGlobal);
348 io.enumCase(value, "hidden", lld::Atom::scopeLinkageUnit);
349 io.enumCase(value, "static", lld::Atom::scopeTranslationUnit);
350 }
351};
352
353template <>
354struct ScalarEnumerationTraits<lld::DefinedAtom::SectionChoice> {
355 static void enumeration(IO &io, lld::DefinedAtom::SectionChoice &value) {
356 io.enumCase(value, "content", lld::DefinedAtom::sectionBasedOnContent);
357 io.enumCase(value, "custom", lld::DefinedAtom::sectionCustomPreferred);
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000358 io.enumCase(value, "custom-required",
Nick Kledzik6b079f52013-01-05 02:22:35 +0000359 lld::DefinedAtom::sectionCustomRequired);
360 }
361};
362
363template <>
Nick Kledzik36293f62013-01-23 22:32:56 +0000364struct ScalarEnumerationTraits<lld::DefinedAtom::SectionPosition> {
365 static void enumeration(IO &io, lld::DefinedAtom::SectionPosition &value) {
366 io.enumCase(value, "start", lld::DefinedAtom::sectionPositionStart);
367 io.enumCase(value, "early", lld::DefinedAtom::sectionPositionEarly);
368 io.enumCase(value, "any", lld::DefinedAtom::sectionPositionAny);
369 io.enumCase(value, "end", lld::DefinedAtom::sectionPositionEnd);
370 }
371};
372
373template <>
Nick Kledzik6b079f52013-01-05 02:22:35 +0000374struct ScalarEnumerationTraits<lld::DefinedAtom::Interposable> {
375 static void enumeration(IO &io, lld::DefinedAtom::Interposable &value) {
376 io.enumCase(value, "no", lld::DefinedAtom::interposeNo);
377 io.enumCase(value, "yes", lld::DefinedAtom::interposeYes);
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000378 io.enumCase(value, "yes-and-weak",
Nick Kledzik6b079f52013-01-05 02:22:35 +0000379 lld::DefinedAtom::interposeYesAndRuntimeWeak);
380 }
381};
382
383template <>
384struct ScalarEnumerationTraits<lld::DefinedAtom::Merge> {
385 static void enumeration(IO &io, lld::DefinedAtom::Merge &value) {
386 io.enumCase(value, "no", lld::DefinedAtom::mergeNo);
387 io.enumCase(value, "as-tentative", lld::DefinedAtom::mergeAsTentative);
388 io.enumCase(value, "as-weak", lld::DefinedAtom::mergeAsWeak);
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000389 io.enumCase(value, "as-addressed-weak",
Nick Kledzik6b079f52013-01-05 02:22:35 +0000390 lld::DefinedAtom::mergeAsWeakAndAddressUsed);
Nick Kledzik233f5372013-01-15 00:17:57 +0000391 io.enumCase(value, "by-content", lld::DefinedAtom::mergeByContent);
Nick Kledzik6b079f52013-01-05 02:22:35 +0000392 }
393};
394
395template <>
396struct ScalarEnumerationTraits<lld::DefinedAtom::DeadStripKind> {
397 static void enumeration(IO &io, lld::DefinedAtom::DeadStripKind &value) {
398 io.enumCase(value, "normal", lld::DefinedAtom::deadStripNormal);
399 io.enumCase(value, "never", lld::DefinedAtom::deadStripNever);
400 io.enumCase(value, "always", lld::DefinedAtom::deadStripAlways);
401 }
402};
403
404template <>
405struct ScalarEnumerationTraits<lld::DefinedAtom::ContentPermissions> {
406 static void enumeration(IO &io, lld::DefinedAtom::ContentPermissions &value) {
Nick Kledzikcc3d2dc2013-01-09 01:17:12 +0000407 io.enumCase(value, "---", lld::DefinedAtom::perm___);
408 io.enumCase(value, "r--", lld::DefinedAtom::permR__);
409 io.enumCase(value, "r-x", lld::DefinedAtom::permR_X);
410 io.enumCase(value, "rw-", lld::DefinedAtom::permRW_);
411 io.enumCase(value, "rwx", lld::DefinedAtom::permRWX);
412 io.enumCase(value, "rw-l", lld::DefinedAtom::permRW_L);
413 io.enumCase(value, "unknown", lld::DefinedAtom::permUnknown);
Nick Kledzik6b079f52013-01-05 02:22:35 +0000414 }
415};
416
417template <>
418struct ScalarEnumerationTraits<lld::DefinedAtom::ContentType> {
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000419 static void enumeration(IO &io, lld::DefinedAtom::ContentType &value) {
420 io.enumCase(value, "unknown",
Nick Kledzik6b079f52013-01-05 02:22:35 +0000421 lld::DefinedAtom::typeUnknown);
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000422 io.enumCase(value, "code",
Nick Kledzik6b079f52013-01-05 02:22:35 +0000423 lld::DefinedAtom::typeCode);
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000424 io.enumCase(value, "stub",
Nick Kledzik6b079f52013-01-05 02:22:35 +0000425 lld::DefinedAtom::typeStub);
Shankar Easwaran873c9ff2013-02-22 17:18:53 +0000426 io.enumCase(value, "constant", lld::DefinedAtom::typeConstant);
427 io.enumCase(value, "data", lld::DefinedAtom::typeData);
428 io.enumCase(value, "quick-data", lld::DefinedAtom::typeDataFast);
429 io.enumCase(value, "zero-fill", lld::DefinedAtom::typeZeroFill);
Shankar Easwarandb74ffb2013-02-24 03:09:10 +0000430 io.enumCase(value, "zero-fill-quick", lld::DefinedAtom::typeZeroFillFast);
Shankar Easwaran873c9ff2013-02-22 17:18:53 +0000431 io.enumCase(value, "const-data", lld::DefinedAtom::typeConstData);
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000432 io.enumCase(value, "got",
Nick Kledzik6b079f52013-01-05 02:22:35 +0000433 lld::DefinedAtom::typeGOT);
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000434 io.enumCase(value, "resolver",
Nick Kledzik6b079f52013-01-05 02:22:35 +0000435 lld::DefinedAtom::typeResolver);
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000436 io.enumCase(value, "branch-island",
Nick Kledzik6b079f52013-01-05 02:22:35 +0000437 lld::DefinedAtom::typeBranchIsland);
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000438 io.enumCase(value, "branch-shim",
Nick Kledzik6b079f52013-01-05 02:22:35 +0000439 lld::DefinedAtom::typeBranchShim);
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000440 io.enumCase(value, "stub-helper",
Nick Kledzik6b079f52013-01-05 02:22:35 +0000441 lld::DefinedAtom::typeStubHelper);
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000442 io.enumCase(value, "c-string",
Nick Kledzik6b079f52013-01-05 02:22:35 +0000443 lld::DefinedAtom::typeCString);
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000444 io.enumCase(value, "utf16-string",
Nick Kledzik6b079f52013-01-05 02:22:35 +0000445 lld::DefinedAtom::typeUTF16String);
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000446 io.enumCase(value, "unwind-cfi",
Nick Kledzik6b079f52013-01-05 02:22:35 +0000447 lld::DefinedAtom::typeCFI);
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000448 io.enumCase(value, "unwind-lsda",
Nick Kledzik6b079f52013-01-05 02:22:35 +0000449 lld::DefinedAtom::typeLSDA);
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000450 io.enumCase(value, "const-4-byte",
Nick Kledzik6b079f52013-01-05 02:22:35 +0000451 lld::DefinedAtom::typeLiteral4);
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000452 io.enumCase(value, "const-8-byte",
Nick Kledzik6b079f52013-01-05 02:22:35 +0000453 lld::DefinedAtom::typeLiteral8);
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000454 io.enumCase(value, "const-16-byte",
Nick Kledzik6b079f52013-01-05 02:22:35 +0000455 lld::DefinedAtom::typeLiteral16);
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000456 io.enumCase(value, "lazy-pointer",
Nick Kledzik6b079f52013-01-05 02:22:35 +0000457 lld::DefinedAtom::typeLazyPointer);
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000458 io.enumCase(value, "lazy-dylib-pointer",
Nick Kledzik6b079f52013-01-05 02:22:35 +0000459 lld::DefinedAtom::typeLazyDylibPointer);
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000460 io.enumCase(value, "cfstring",
Nick Kledzik6b079f52013-01-05 02:22:35 +0000461 lld::DefinedAtom::typeCFString);
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000462 io.enumCase(value, "initializer-pointer",
Nick Kledzik6b079f52013-01-05 02:22:35 +0000463 lld::DefinedAtom::typeInitializerPtr);
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000464 io.enumCase(value, "terminator-pointer",
Nick Kledzik6b079f52013-01-05 02:22:35 +0000465 lld::DefinedAtom::typeTerminatorPtr);
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000466 io.enumCase(value, "c-string-pointer",
Nick Kledzik6b079f52013-01-05 02:22:35 +0000467 lld::DefinedAtom::typeCStringPtr);
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000468 io.enumCase(value, "objc-class-pointer",
Nick Kledzik6b079f52013-01-05 02:22:35 +0000469 lld::DefinedAtom::typeObjCClassPtr);
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000470 io.enumCase(value, "objc-category-list",
Nick Kledzik6b079f52013-01-05 02:22:35 +0000471 lld::DefinedAtom::typeObjC2CategoryList);
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000472 io.enumCase(value, "objc-class1",
Nick Kledzik6b079f52013-01-05 02:22:35 +0000473 lld::DefinedAtom::typeObjC1Class);
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000474 io.enumCase(value, "dtraceDOF",
Nick Kledzik6b079f52013-01-05 02:22:35 +0000475 lld::DefinedAtom::typeDTraceDOF);
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000476 io.enumCase(value, "lto-temp",
Nick Kledzik6b079f52013-01-05 02:22:35 +0000477 lld::DefinedAtom::typeTempLTO);
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000478 io.enumCase(value, "compact-unwind",
Nick Kledzik6b079f52013-01-05 02:22:35 +0000479 lld::DefinedAtom::typeCompactUnwindInfo);
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000480 io.enumCase(value, "tlv-thunk",
Nick Kledzik6b079f52013-01-05 02:22:35 +0000481 lld::DefinedAtom::typeThunkTLV);
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000482 io.enumCase(value, "tlv-data",
Nick Kledzik6b079f52013-01-05 02:22:35 +0000483 lld::DefinedAtom::typeTLVInitialData);
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000484 io.enumCase(value, "tlv-zero-fill",
Nick Kledzik6b079f52013-01-05 02:22:35 +0000485 lld::DefinedAtom::typeTLVInitialZeroFill);
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000486 io.enumCase(value, "tlv-initializer-ptr",
Nick Kledzik6b079f52013-01-05 02:22:35 +0000487 lld::DefinedAtom::typeTLVInitializerPtr);
Nick Kledzik6b079f52013-01-05 02:22:35 +0000488 }
489};
490
491template <>
492struct ScalarEnumerationTraits<lld::UndefinedAtom::CanBeNull> {
493 static void enumeration(IO &io, lld::UndefinedAtom::CanBeNull &value) {
494 io.enumCase(value, "never", lld::UndefinedAtom::canBeNullNever);
495 io.enumCase(value, "at-runtime", lld::UndefinedAtom::canBeNullAtRuntime);
496 io.enumCase(value, "at-buildtime", lld::UndefinedAtom::canBeNullAtBuildtime);
497 }
498};
499
500
501template <>
502struct ScalarEnumerationTraits<ShlibCanBeNull> {
503 static void enumeration(IO &io, ShlibCanBeNull &value) {
504 io.enumCase(value, "never", false);
505 io.enumCase(value, "at-runtime", true);
506 }
507};
508
509
510
511/// This is a custom formatter for lld::DefinedAtom::Alignment. Values look
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000512/// like:
Nick Kledzik6b079f52013-01-05 02:22:35 +0000513/// 2^3 # 8-byte aligned
514/// 7 mod 2^4 # 16-byte aligned plus 7 bytes
515template<>
516struct ScalarTraits<lld::DefinedAtom::Alignment> {
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000517 static void output(const lld::DefinedAtom::Alignment &value, void *ctxt,
Nick Kledzik6b079f52013-01-05 02:22:35 +0000518 llvm::raw_ostream &out) {
519 if (value.modulus == 0) {
520 out << llvm::format("2^%d", value.powerOf2);
521 }
522 else {
523 out << llvm::format("%d mod 2^%d", value.modulus, value.powerOf2);
524 }
525 }
526
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000527 static StringRef input(StringRef scalar, void *ctxt,
Nick Kledzik6b079f52013-01-05 02:22:35 +0000528 lld::DefinedAtom::Alignment &value) {
529 value.modulus = 0;
530 size_t modStart = scalar.find("mod");
531 if (modStart != StringRef::npos) {
532 StringRef modStr = scalar.slice(0, modStart);
533 modStr = modStr.rtrim();
534 unsigned int modulus;
535 if (modStr.getAsInteger(0, modulus)) {
536 return "malformed alignment modulus";
537 }
538 value.modulus = modulus;
539 scalar = scalar.drop_front(modStart+3);
540 scalar = scalar.ltrim();
541 }
542 if (!scalar.startswith("2^")) {
543 return "malformed alignment";
544 }
545 StringRef powerStr = scalar.drop_front(2);
546 unsigned int power;
547 if (powerStr.getAsInteger(0, power)) {
548 return "malformed alignment power";
549 }
550 value.powerOf2 = power;
551 if (value.modulus > (1<<value.powerOf2)) {
552 return "malformed alignment, modulus too large for power";
553 }
554 return StringRef(); // returning empty string means success
555 }
556};
557
558
559
560
561template <>
562struct ScalarEnumerationTraits<FileKinds> {
563 static void enumeration(IO &io, FileKinds &value) {
564 io.enumCase(value, "object", fileKindObjectAtoms);
565 io.enumCase(value, "archive", fileKindArchive);
566 io.enumCase(value, "object-elf", fileKindObjectELF);
567 io.enumCase(value, "object-mach-o", fileKindObjectMachO);
568 }
569};
570
571template <>
572struct MappingTraits<ArchMember> {
573 static void mapping(IO &io, ArchMember &member) {
574 io.mapOptional("kind", member._kind, fileKindObjectAtoms);
575 io.mapOptional("name", member._name);
576 io.mapRequired("content", member._content);
577 }
578};
579
Nick Kledzik6b079f52013-01-05 02:22:35 +0000580
581
582// Declare that an AtomList is a yaml sequence.
583template<typename T>
584struct SequenceTraits<AtomList<T>> {
585 static size_t size(IO &io, AtomList<T> &seq) {
586 return seq._atoms.size();
587 }
588 static const T *&element(IO &io, AtomList<T> &seq, size_t index) {
589 if (index >= seq._atoms.size())
590 seq._atoms.resize(index+1);
591 return seq._atoms[index];
592 }
593};
594
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000595// Used to allow DefinedAtom content bytes to be a flow sequence of
Nick Kledzik6b079f52013-01-05 02:22:35 +0000596// two-digit hex numbers without the leading 0x (e.g. FF, 04, 0A)
597template<>
598struct ScalarTraits<ImplicitHex8> {
599 static void output(const ImplicitHex8 &val, void*, llvm::raw_ostream &out) {
600 uint8_t num = val;
601 out << llvm::format("%02X", num);
602 }
603
604 static llvm::StringRef input(llvm::StringRef str, void*, ImplicitHex8 &val) {
605 unsigned long long n;
606 if (getAsUnsignedInteger(str, 16, n))
607 return "invalid two-digit-hex number";
608 if (n > 0xFF)
609 return "out of range two-digit-hex number";
610 val = n;
611 return StringRef(); // returning empty string means success
612 }
613};
614
Nick Kledzik6b079f52013-01-05 02:22:35 +0000615
616// YAML conversion for std::vector<const lld::File*>
617template<>
618struct DocumentListTraits< std::vector<const lld::File*> > {
619 static size_t size(IO &io, std::vector<const lld::File*> &seq) {
620 return seq.size();
621 }
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000622 static const lld::File *&element(IO &io, std::vector<const lld::File*> &seq,
Nick Kledzik6b079f52013-01-05 02:22:35 +0000623 size_t index) {
624 if (index >= seq.size())
625 seq.resize(index+1);
626 return seq[index];
627 }
628};
629
630
631// YAML conversion for const lld::File*
632template <>
633struct MappingTraits<const lld::File*> {
Shankar Easwaran3256d4f2013-01-25 07:39:18 +0000634
635 class NormArchiveFile : public lld::ArchiveLibraryFile {
636 public:
637 NormArchiveFile(IO &io)
638 : ArchiveLibraryFile(((ContextInfo *)io.getContext())->_targetInfo,
639 ""),
640 _path() {
641 }
642 NormArchiveFile(IO &io, const lld::File *file)
643 : ArchiveLibraryFile(((ContextInfo *)io.getContext())->_targetInfo,
644 file->path()),
645 _path(file->path()) {
Nick Kledzik6b079f52013-01-05 02:22:35 +0000646 // If we want to support writing archives, this constructor would
647 // need to populate _members.
Shankar Easwaran3256d4f2013-01-25 07:39:18 +0000648 }
Nick Kledzik6b079f52013-01-05 02:22:35 +0000649
650 const lld::File *denormalize(IO &io) {
651 return this;
652 }
Michael J. Spencer57752dc2013-01-12 02:45:54 +0000653
Nick Kledzik36293f62013-01-23 22:32:56 +0000654 virtual void setOrdinalAndIncrement(uint64_t &ordinal) const {
655 _ordinal = ordinal++;
656 // Assign sequential ordinals to member files
657 for (const ArchMember &member : _members) {
658 member._content->setOrdinalAndIncrement(ordinal);
659 }
660 }
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000661
Nick Kledzik6b079f52013-01-05 02:22:35 +0000662 virtual const atom_collection<lld::DefinedAtom> &defined() const {
663 return _noDefinedAtoms;
664 }
665 virtual const atom_collection<lld::UndefinedAtom> &undefined() const {
666 return _noUndefinedAtoms;
667 }
668 virtual const atom_collection<lld::SharedLibraryAtom> &sharedLibrary()const{
669 return _noSharedLibaryAtoms;
670 }
671 virtual const atom_collection<lld::AbsoluteAtom> &absolute() const {
672 return _noAbsoluteAtoms;
673 }
674 virtual const File *find(StringRef name, bool dataSymbolOnly) const {
675 for (const ArchMember &member : _members) {
676 for (const lld::DefinedAtom *atom : member._content->defined() ) {
677 if (name == atom->name()) {
678 if (!dataSymbolOnly)
679 return member._content;
680 switch (atom->contentType()) {
681 case lld::DefinedAtom::typeData:
682 case lld::DefinedAtom::typeZeroFill:
683 return member._content;
684 default:
685 break;
686 }
687 }
688 }
689 }
690 return nullptr;
691 }
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000692
Nick Kledzik6b079f52013-01-05 02:22:35 +0000693 StringRef _path;
694 std::vector<ArchMember> _members;
695 };
Shankar Easwaran3256d4f2013-01-25 07:39:18 +0000696
697 class NormalizedFile : public lld::File {
698 public:
Michael J. Spencer0f3dd612013-03-20 18:57:27 +0000699 NormalizedFile(IO &io) : File("", kindObject), _IO(io), _rnb(nullptr) {}
Shankar Easwaran3256d4f2013-01-25 07:39:18 +0000700 NormalizedFile(IO &io, const lld::File *file)
Michael J. Spencer0f3dd612013-03-20 18:57:27 +0000701 : File(file->path(), kindObject), _IO(io), _rnb(new RefNameBuilder(*file)),
Shankar Easwaran3256d4f2013-01-25 07:39:18 +0000702 _path(file->path()) {
703 for (const lld::DefinedAtom *a : file->defined())
704 _definedAtoms.push_back(a);
705 for (const lld::UndefinedAtom *a : file->undefined())
Nick Kledzik6b079f52013-01-05 02:22:35 +0000706 _undefinedAtoms.push_back(a);
707 for (const lld::SharedLibraryAtom *a : file->sharedLibrary())
708 _sharedLibraryAtoms.push_back(a);
709 for (const lld::AbsoluteAtom *a : file->absolute())
710 _absoluteAtoms.push_back(a);
711 }
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000712 const lld::File *denormalize(IO &io);
Michael J. Spencer57752dc2013-01-12 02:45:54 +0000713
Nick Kledzik6b079f52013-01-05 02:22:35 +0000714 virtual const atom_collection<lld::DefinedAtom> &defined() const {
715 return _definedAtoms;
716 }
717 virtual const atom_collection<lld::UndefinedAtom> &undefined() const {
718 return _undefinedAtoms;
719 }
720 virtual const atom_collection<lld::SharedLibraryAtom> &sharedLibrary()const{
721 return _sharedLibraryAtoms;
722 }
Shankar Easwaran3256d4f2013-01-25 07:39:18 +0000723 virtual const atom_collection<lld::AbsoluteAtom> &absolute() const {
724 return _absoluteAtoms;
725 }
726
727 virtual const TargetInfo &getTargetInfo() const {
728 return ((ContextInfo *)_IO.getContext())->_targetInfo;
729 }
730
731 // Allocate a new copy of this string and keep track of allocations
732 // in _stringCopies, so they can be freed when File is destroyed.
Nick Kledzik6b079f52013-01-05 02:22:35 +0000733 StringRef copyString(StringRef str) {
734 // We want _stringCopies to own the string memory so it is deallocated
735 // when the File object is destroyed. But we need a StringRef that
736 // points into that memory.
Michael J. Spencer20231f12013-01-26 12:26:56 +0000737 std::unique_ptr<char[]> s(new char[str.size()]);
Nick Kledzik6b079f52013-01-05 02:22:35 +0000738 memcpy(s.get(), str.data(), str.size());
739 llvm::StringRef r = llvm::StringRef(s.get(), str.size());
740 _stringCopies.push_back(std::move(s));
Shankar Easwaran3256d4f2013-01-25 07:39:18 +0000741 return r;
742 }
743
744 IO &_IO;
745 RefNameBuilder *_rnb;
746 StringRef _path;
747 AtomList<lld::DefinedAtom> _definedAtoms;
Nick Kledzik6b079f52013-01-05 02:22:35 +0000748 AtomList<lld::UndefinedAtom> _undefinedAtoms;
749 AtomList<lld::SharedLibraryAtom> _sharedLibraryAtoms;
750 AtomList<lld::AbsoluteAtom> _absoluteAtoms;
Michael J. Spencer20231f12013-01-26 12:26:56 +0000751 std::vector<std::unique_ptr<char[]>> _stringCopies;
Nick Kledzik6b079f52013-01-05 02:22:35 +0000752 };
753
754
755 static void mapping(IO &io, const lld::File *&file) {
756 // We only support writing atom based YAML
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000757 FileKinds kind = fileKindObjectAtoms;
Nick Kledzik6b079f52013-01-05 02:22:35 +0000758 // If reading, peek ahead to see what kind of file this is.
759 io.mapOptional("kind", kind, fileKindObjectAtoms);
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000760 //
Nick Kledzik6b079f52013-01-05 02:22:35 +0000761 switch (kind) {
762 case fileKindObjectAtoms:
763 mappingAtoms(io, file);
764 break;
765 case fileKindArchive:
766 mappingArchive(io, file);
767 break;
768 case fileKindObjectELF:
769 case fileKindObjectMachO:
770 // Eventually we will have an external function to call, similar
771 // to mappingAtoms() and mappingArchive() but implememented
772 // with coresponding file format code.
Nick Kledzik6b079f52013-01-05 02:22:35 +0000773 llvm_unreachable("section based YAML not supported yet");
774 }
775 }
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000776
Nick Kledzik6b079f52013-01-05 02:22:35 +0000777 static void mappingAtoms(IO &io, const lld::File *&file) {
778 MappingNormalizationHeap<NormalizedFile, const lld::File*> keys(io, file);
779 ContextInfo *info = reinterpret_cast<ContextInfo*>(io.getContext());
780 assert(info != nullptr);
781 info->_currentFile = keys.operator->();
782
783 io.mapOptional("path", keys->_path);
784 io.mapOptional("defined-atoms", keys->_definedAtoms);
785 io.mapOptional("undefined-atoms", keys->_undefinedAtoms);
786 io.mapOptional("shared-library-atoms", keys->_sharedLibraryAtoms);
787 io.mapOptional("absolute-atoms", keys->_absoluteAtoms);
788 }
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000789
Nick Kledzik6b079f52013-01-05 02:22:35 +0000790 static void mappingArchive(IO &io, const lld::File *&file) {
791 MappingNormalizationHeap<NormArchiveFile, const lld::File*> keys(io, file);
792
793 io.mapOptional("path", keys->_path);
794 io.mapOptional("members", keys->_members);
795 }
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000796
Nick Kledzik6b079f52013-01-05 02:22:35 +0000797};
798
799
800
801// YAML conversion for const lld::Reference*
802template <>
803struct MappingTraits<const lld::Reference*> {
804
805 class NormalizedReference : public lld::Reference {
806 public:
807 NormalizedReference(IO &io)
Michael J. Spencerfa405272013-03-20 18:57:52 +0000808 : _target(nullptr), _targetName(), _offset(0), _addend(0) {}
809
Nick Kledzik6b079f52013-01-05 02:22:35 +0000810 NormalizedReference(IO &io, const lld::Reference *ref)
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000811 : _target(nullptr),
812 _targetName(targetName(io, ref)),
813 _offset(ref->offsetInAtom()),
Nick Kledzik6b079f52013-01-05 02:22:35 +0000814 _addend(ref->addend()),
Michael J. Spencerfa405272013-03-20 18:57:52 +0000815 _mappedKind(ref->kind()) {
Nick Kledzik6b079f52013-01-05 02:22:35 +0000816 }
Michael J. Spencerfa405272013-03-20 18:57:52 +0000817
Nick Kledzik6b079f52013-01-05 02:22:35 +0000818 const lld::Reference *denormalize(IO &io) {
819 ContextInfo *info = reinterpret_cast<ContextInfo*>(io.getContext());
820 assert(info != nullptr);
821 typedef MappingTraits<const lld::File*>::NormalizedFile NormalizedFile;
822 NormalizedFile *f = reinterpret_cast<NormalizedFile*>(info->_currentFile);
823 if (!_targetName.empty())
824 _targetName = f->copyString(_targetName);
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000825 DEBUG_WITH_TYPE("WriterYAML", llvm::dbgs()
826 << "created Reference to name: '" << _targetName
827 << "' (" << (void*)_targetName.data() << ", "
Nick Kledzik6b079f52013-01-05 02:22:35 +0000828 << _targetName.size() << ")\n");
Michael J. Spencerfa405272013-03-20 18:57:52 +0000829 setKind(_mappedKind);
Nick Kledzik6b079f52013-01-05 02:22:35 +0000830 return this;
831 }
832 void bind(const RefNameResolver&);
833 static StringRef targetName(IO &io, const lld::Reference *ref);
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000834
Nick Kledzik6b079f52013-01-05 02:22:35 +0000835 virtual uint64_t offsetInAtom() const { return _offset; }
Nick Kledzik6b079f52013-01-05 02:22:35 +0000836 virtual const lld::Atom *target() const { return _target; }
837 virtual Addend addend() const { return _addend; }
Nick Kledzik6b079f52013-01-05 02:22:35 +0000838 virtual void setAddend(Addend a) { _addend = a; }
839 virtual void setTarget(const lld::Atom *a) { _target = a; }
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000840
Nick Kledzik6b079f52013-01-05 02:22:35 +0000841 const lld::Atom *_target;
842 StringRef _targetName;
843 uint32_t _offset;
844 Addend _addend;
Michael J. Spencerfa405272013-03-20 18:57:52 +0000845 RefKind _mappedKind;
Nick Kledzik6b079f52013-01-05 02:22:35 +0000846 };
847
848
849 static void mapping(IO &io, const lld::Reference *&ref) {
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000850 MappingNormalizationHeap<NormalizedReference,
Nick Kledzik6b079f52013-01-05 02:22:35 +0000851 const lld::Reference*> keys(io, ref);
852
Michael J. Spencerfa405272013-03-20 18:57:52 +0000853 io.mapRequired("kind", keys->_mappedKind);
Nick Kledzik6b079f52013-01-05 02:22:35 +0000854 io.mapOptional("offset", keys->_offset);
855 io.mapOptional("target", keys->_targetName);
856 io.mapOptional("addend", keys->_addend, (lld::Reference::Addend)0);
857 }
858};
859
Nick Kledzik6b079f52013-01-05 02:22:35 +0000860
861
862// YAML conversion for const lld::DefinedAtom*
863template <>
864struct MappingTraits<const lld::DefinedAtom*> {
865
866 class NormalizedAtom : public lld::DefinedAtom {
867 public:
868 NormalizedAtom(IO &io)
Michael J. Spencer096ea032013-04-05 21:07:44 +0000869 : _file(fileFromContext(io)), _name(), _refName(), _contentType(),
Nick Kledzik6b079f52013-01-05 02:22:35 +0000870 _alignment(0), _content(), _references() {
Nick Kledzik36293f62013-01-23 22:32:56 +0000871 static uint32_t ordinalCounter = 1;
872 _ordinal = ordinalCounter++;
Nick Kledzik6b079f52013-01-05 02:22:35 +0000873 }
874 NormalizedAtom(IO &io, const lld::DefinedAtom *atom)
875 : _file(fileFromContext(io)),
876 _name(atom->name()),
877 _refName(),
878 _scope(atom->scope()),
879 _interpose(atom->interposable()),
880 _merge(atom->merge()),
881 _contentType(atom->contentType()),
882 _alignment(atom->alignment()),
883 _sectionChoice(atom->sectionChoice()),
Nick Kledzik36293f62013-01-23 22:32:56 +0000884 _sectionPosition(atom->sectionPosition()),
Nick Kledzik6b079f52013-01-05 02:22:35 +0000885 _deadStrip(atom->deadStrip()),
886 _permissions(atom->permissions()),
887 _size(atom->size()),
888 _sectionName(atom->customSectionName()) {
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000889 for ( const lld::Reference *r : *atom )
Nick Kledzik6b079f52013-01-05 02:22:35 +0000890 _references.push_back(r);
891 ArrayRef<uint8_t> cont = atom->rawContent();
892 _content.reserve(cont.size());
893 for (uint8_t x : cont)
894 _content.push_back(x);
895 }
896 const lld::DefinedAtom *denormalize(IO &io) {
897 ContextInfo *info = reinterpret_cast<ContextInfo*>(io.getContext());
898 assert(info != nullptr);
899 typedef MappingTraits<const lld::File*>::NormalizedFile NormalizedFile;
900 NormalizedFile *f = reinterpret_cast<NormalizedFile*>(info->_currentFile);
901 if ( !_name.empty() )
902 _name = f->copyString(_name);
903 if ( !_refName.empty() )
904 _refName = f->copyString(_refName);
905 if ( !_sectionName.empty() )
906 _sectionName = f->copyString(_sectionName);
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000907 DEBUG_WITH_TYPE("WriterYAML", llvm::dbgs()
908 << "created DefinedAtom named: '" << _name
909 << "' (" << (void*)_name.data() << ", "
Nick Kledzik6b079f52013-01-05 02:22:35 +0000910 << _name.size() << ")\n");
911 return this;
912 }
913 void bind(const RefNameResolver&);
914 // Extract current File object from YAML I/O parsing context
915 const lld::File &fileFromContext(IO &io) {
916 ContextInfo *info = reinterpret_cast<ContextInfo*>(io.getContext());
917 assert(info != nullptr);
918 assert(info->_currentFile != nullptr);
919 return *info->_currentFile;
920 }
921
922 virtual const lld::File &file() const { return _file; }
923 virtual StringRef name() const { return _name; }
924 virtual uint64_t size() const { return _size; }
925 virtual Scope scope() const { return _scope; }
926 virtual Interposable interposable() const { return _interpose; }
927 virtual Merge merge() const { return _merge; }
928 virtual ContentType contentType() const { return _contentType; }
929 virtual Alignment alignment() const { return _alignment; }
930 virtual SectionChoice sectionChoice() const { return _sectionChoice; }
931 virtual StringRef customSectionName() const { return _sectionName;}
Nick Kledzik36293f62013-01-23 22:32:56 +0000932 virtual SectionPosition sectionPosition() const{return _sectionPosition;}
Nick Kledzik6b079f52013-01-05 02:22:35 +0000933 virtual DeadStripKind deadStrip() const { return _deadStrip; }
934 virtual ContentPermissions permissions() const { return _permissions; }
935 virtual bool isThumb() const { return false; }
936 virtual bool isAlias() const { return false; }
Michael J. Spencer74ba7222013-01-13 01:09:39 +0000937 ArrayRef<uint8_t> rawContent() const {
938 return ArrayRef<uint8_t>(
939 reinterpret_cast<const uint8_t *>(_content.data()), _content.size());
940 }
941
Nick Kledzik36293f62013-01-23 22:32:56 +0000942 virtual uint64_t ordinal() const { return _ordinal; }
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000943
944 reference_iterator begin() const {
Nick Kledzik6b079f52013-01-05 02:22:35 +0000945 uintptr_t index = 0;
946 const void *it = reinterpret_cast<const void*>(index);
947 return reference_iterator(*this, it);
948 }
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000949 reference_iterator end() const {
Nick Kledzik6b079f52013-01-05 02:22:35 +0000950 uintptr_t index = _references.size();
951 const void *it = reinterpret_cast<const void*>(index);
952 return reference_iterator(*this, it);
953 }
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000954 const lld::Reference *derefIterator(const void *it) const {
Nick Kledzik6b079f52013-01-05 02:22:35 +0000955 uintptr_t index = reinterpret_cast<uintptr_t>(it);
956 assert(index < _references.size());
957 return _references[index];
958 }
959 void incrementIterator(const void *&it) const {
960 uintptr_t index = reinterpret_cast<uintptr_t>(it);
961 ++index;
962 it = reinterpret_cast<const void*>(index);
963 }
964
965 const lld::File &_file;
966 StringRef _name;
967 StringRef _refName;
968 Scope _scope;
969 Interposable _interpose;
970 Merge _merge;
971 ContentType _contentType;
972 Alignment _alignment;
973 SectionChoice _sectionChoice;
Nick Kledzik36293f62013-01-23 22:32:56 +0000974 SectionPosition _sectionPosition;
Nick Kledzik6b079f52013-01-05 02:22:35 +0000975 DeadStripKind _deadStrip;
976 ContentPermissions _permissions;
Nick Kledzik36293f62013-01-23 22:32:56 +0000977 uint32_t _ordinal;
Nick Kledzik6b079f52013-01-05 02:22:35 +0000978 std::vector<ImplicitHex8> _content;
979 uint64_t _size;
980 StringRef _sectionName;
981 std::vector<const lld::Reference*> _references;
982 };
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000983
Nick Kledzik6b079f52013-01-05 02:22:35 +0000984 static void mapping(IO &io, const lld::DefinedAtom *&atom) {
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000985 MappingNormalizationHeap<NormalizedAtom,
Nick Kledzik6b079f52013-01-05 02:22:35 +0000986 const lld::DefinedAtom*> keys(io, atom);
987 if ( io.outputting() ) {
988 // If writing YAML, check if atom needs a ref-name.
989 typedef MappingTraits<const lld::File*>::NormalizedFile NormalizedFile;
990 ContextInfo *info = reinterpret_cast<ContextInfo*>(io.getContext());
991 assert(info != nullptr);
992 NormalizedFile *f = reinterpret_cast<NormalizedFile*>(info->_currentFile);
993 assert(f);
994 assert(f->_rnb);
995 if ( f->_rnb->hasRefName(atom) ) {
996 keys->_refName = f->_rnb->refName(atom);
997 }
998 }
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000999
1000 io.mapOptional("name", keys->_name,
Nick Kledzik6b079f52013-01-05 02:22:35 +00001001 StringRef());
Shankar Easwaran8962feb2013-03-14 16:09:49 +00001002 io.mapOptional("ref-name", keys->_refName,
Nick Kledzik6b079f52013-01-05 02:22:35 +00001003 StringRef());
Shankar Easwaran8962feb2013-03-14 16:09:49 +00001004 io.mapOptional("scope", keys->_scope,
Nick Kledzik6b079f52013-01-05 02:22:35 +00001005 lld::DefinedAtom::scopeTranslationUnit);
Shankar Easwaran8962feb2013-03-14 16:09:49 +00001006 io.mapOptional("type", keys->_contentType,
Nick Kledzik6b079f52013-01-05 02:22:35 +00001007 lld::DefinedAtom::typeCode);
1008 io.mapOptional("content", keys->_content);
Shankar Easwaran8962feb2013-03-14 16:09:49 +00001009 io.mapOptional("size", keys->_size,
Nick Kledzik6b079f52013-01-05 02:22:35 +00001010 (uint64_t)keys->_content.size());
Shankar Easwaran8962feb2013-03-14 16:09:49 +00001011 io.mapOptional("interposable", keys->_interpose,
Nick Kledzik6b079f52013-01-05 02:22:35 +00001012 lld::DefinedAtom::interposeNo);
Shankar Easwaran8962feb2013-03-14 16:09:49 +00001013 io.mapOptional("merge", keys->_merge,
Nick Kledzik6b079f52013-01-05 02:22:35 +00001014 lld::DefinedAtom::mergeNo);
Shankar Easwaran8962feb2013-03-14 16:09:49 +00001015 io.mapOptional("alignment", keys->_alignment,
Nick Kledzik6b079f52013-01-05 02:22:35 +00001016 lld::DefinedAtom::Alignment(0));
Shankar Easwaran8962feb2013-03-14 16:09:49 +00001017 io.mapOptional("section-choice", keys->_sectionChoice,
Nick Kledzik6b079f52013-01-05 02:22:35 +00001018 lld::DefinedAtom::sectionBasedOnContent);
Shankar Easwaran8962feb2013-03-14 16:09:49 +00001019 io.mapOptional("section-name", keys->_sectionName,
Nick Kledzik6b079f52013-01-05 02:22:35 +00001020 StringRef());
Shankar Easwaran8962feb2013-03-14 16:09:49 +00001021 io.mapOptional("section-position",keys->_sectionPosition,
Nick Kledzik36293f62013-01-23 22:32:56 +00001022 lld::DefinedAtom::sectionPositionAny);
Shankar Easwaran8962feb2013-03-14 16:09:49 +00001023 io.mapOptional("dead-strip", keys->_deadStrip,
Nick Kledzik6b079f52013-01-05 02:22:35 +00001024 lld::DefinedAtom::deadStripNormal);
Nick Kledzikcc3d2dc2013-01-09 01:17:12 +00001025 // default permissions based on content type
Shankar Easwaran8962feb2013-03-14 16:09:49 +00001026 io.mapOptional("permissions", keys->_permissions,
Nick Kledzikcc3d2dc2013-01-09 01:17:12 +00001027 lld::DefinedAtom::permissions(
1028 keys->_contentType));
Nick Kledzik8a3052e2013-01-08 21:12:13 +00001029 io.mapOptional("references", keys->_references);
Nick Kledzik6b079f52013-01-05 02:22:35 +00001030 }
1031};
1032
1033
1034
1035
Nick Kledzik6b079f52013-01-05 02:22:35 +00001036// YAML conversion for const lld::UndefinedAtom*
1037template <>
1038struct MappingTraits<const lld::UndefinedAtom*> {
1039
1040 class NormalizedAtom : public lld::UndefinedAtom {
1041 public:
1042 NormalizedAtom(IO &io)
1043 : _file(fileFromContext(io)), _name(), _canBeNull(canBeNullNever) {
1044 }
1045 NormalizedAtom(IO &io, const lld::UndefinedAtom *atom)
Shankar Easwaran8962feb2013-03-14 16:09:49 +00001046 : _file(fileFromContext(io)),
1047 _name(atom->name()),
Nick Kledzik6b079f52013-01-05 02:22:35 +00001048 _canBeNull(atom->canBeNull()) {
1049 }
1050 const lld::UndefinedAtom *denormalize(IO &io) {
1051 ContextInfo *info = reinterpret_cast<ContextInfo*>(io.getContext());
1052 assert(info != nullptr);
1053 typedef MappingTraits<const lld::File*>::NormalizedFile NormalizedFile;
1054 NormalizedFile *f = reinterpret_cast<NormalizedFile*>(info->_currentFile);
1055 if ( !_name.empty() )
1056 _name = f->copyString(_name);
1057
Shankar Easwaran8962feb2013-03-14 16:09:49 +00001058 DEBUG_WITH_TYPE("WriterYAML", llvm::dbgs()
1059 << "created UndefinedAtom named: '" << _name
1060 << "' (" << (void*)_name.data() << ", "
Nick Kledzik6b079f52013-01-05 02:22:35 +00001061 << _name.size() << ")\n");
1062 return this;
1063 }
1064 // Extract current File object from YAML I/O parsing context
1065 const lld::File &fileFromContext(IO &io) {
1066 ContextInfo *info = reinterpret_cast<ContextInfo*>(io.getContext());
1067 assert(info != nullptr);
1068 assert(info->_currentFile != nullptr);
1069 return *info->_currentFile;
1070 }
1071
1072 virtual const lld::File &file() const { return _file; }
1073 virtual StringRef name() const { return _name; }
1074 virtual CanBeNull canBeNull() const { return _canBeNull; }
Shankar Easwaran8962feb2013-03-14 16:09:49 +00001075
Nick Kledzik6b079f52013-01-05 02:22:35 +00001076 const lld::File &_file;
1077 StringRef _name;
1078 CanBeNull _canBeNull;
1079 };
1080
1081
1082 static void mapping(IO &io, const lld::UndefinedAtom* &atom) {
Shankar Easwaran8962feb2013-03-14 16:09:49 +00001083 MappingNormalizationHeap<NormalizedAtom,
Nick Kledzik6b079f52013-01-05 02:22:35 +00001084 const lld::UndefinedAtom*> keys(io, atom);
1085
1086 io.mapRequired("name", keys->_name);
Shankar Easwaran8962feb2013-03-14 16:09:49 +00001087 io.mapOptional("can-be-null", keys->_canBeNull,
Nick Kledzik6b079f52013-01-05 02:22:35 +00001088 lld::UndefinedAtom::canBeNullNever);
1089 }
1090};
1091
1092
Nick Kledzik36293f62013-01-23 22:32:56 +00001093
Nick Kledzik6b079f52013-01-05 02:22:35 +00001094// YAML conversion for const lld::SharedLibraryAtom*
1095template <>
1096struct MappingTraits<const lld::SharedLibraryAtom*> {
1097
1098 class NormalizedAtom : public lld::SharedLibraryAtom {
1099 public:
1100 NormalizedAtom(IO &io)
1101 : _file(fileFromContext(io)), _name(), _loadName(), _canBeNull(false) {
1102 }
1103 NormalizedAtom(IO &io, const lld::SharedLibraryAtom *atom)
Shankar Easwaran8962feb2013-03-14 16:09:49 +00001104 : _file(fileFromContext(io)),
1105 _name(atom->name()),
1106 _loadName(atom->loadName()),
Nick Kledzik6b079f52013-01-05 02:22:35 +00001107 _canBeNull(atom->canBeNullAtRuntime()) {
1108 }
1109 const lld::SharedLibraryAtom *denormalize(IO &io) {
1110 ContextInfo *info = reinterpret_cast<ContextInfo*>(io.getContext());
1111 assert(info != nullptr);
1112 typedef MappingTraits<const lld::File*>::NormalizedFile NormalizedFile;
1113 NormalizedFile *f = reinterpret_cast<NormalizedFile*>(info->_currentFile);
1114 if ( !_name.empty() )
1115 _name = f->copyString(_name);
1116 if ( !_loadName.empty() )
1117 _loadName = f->copyString(_loadName);
1118
Shankar Easwaran8962feb2013-03-14 16:09:49 +00001119 DEBUG_WITH_TYPE("WriterYAML", llvm::dbgs()
1120 << "created SharedLibraryAtom named: '" << _name
1121 << "' (" << (void*)_name.data() << ", "
Nick Kledzik6b079f52013-01-05 02:22:35 +00001122 << _name.size() << ")\n");
1123 return this;
1124 }
1125 // Extract current File object from YAML I/O parsing context
1126 const lld::File &fileFromContext(IO &io) {
1127 ContextInfo *info = reinterpret_cast<ContextInfo*>(io.getContext());
1128 assert(info != nullptr);
1129 assert(info->_currentFile != nullptr);
1130 return *info->_currentFile;
1131 }
1132
1133 virtual const lld::File &file() const { return _file; }
1134 virtual StringRef name() const { return _name; }
1135 virtual StringRef loadName() const { return _loadName;}
1136 virtual bool canBeNullAtRuntime() const { return _canBeNull; }
Shankar Easwaran8962feb2013-03-14 16:09:49 +00001137
Nick Kledzik6b079f52013-01-05 02:22:35 +00001138 const lld::File &_file;
1139 StringRef _name;
1140 StringRef _loadName;
1141 ShlibCanBeNull _canBeNull;
1142 };
1143
1144
1145 static void mapping(IO &io, const lld::SharedLibraryAtom *&atom) {
Shankar Easwaran8962feb2013-03-14 16:09:49 +00001146
1147 MappingNormalizationHeap<NormalizedAtom,
Nick Kledzik6b079f52013-01-05 02:22:35 +00001148 const lld::SharedLibraryAtom*> keys(io, atom);
1149
1150 io.mapRequired("name", keys->_name);
1151 io.mapOptional("load-name", keys->_loadName);
Shankar Easwaran8962feb2013-03-14 16:09:49 +00001152 io.mapOptional("can-be-null", keys->_canBeNull,
Nick Kledzik6b079f52013-01-05 02:22:35 +00001153 (ShlibCanBeNull)false);
1154 }
1155};
1156
1157
1158// YAML conversion for const lld::AbsoluteAtom*
1159template <>
1160struct MappingTraits<const lld::AbsoluteAtom*> {
1161
1162 class NormalizedAtom : public lld::AbsoluteAtom {
1163 public:
1164 NormalizedAtom(IO &io)
1165 : _file(fileFromContext(io)), _name(), _scope(), _value(0) {
1166 }
1167 NormalizedAtom(IO &io, const lld::AbsoluteAtom *atom)
Shankar Easwaran8962feb2013-03-14 16:09:49 +00001168 : _file(fileFromContext(io)),
1169 _name(atom->name()),
Nick Kledzik6b079f52013-01-05 02:22:35 +00001170 _scope(atom->scope()),
1171 _value(atom->value()) {
1172 }
1173 const lld::AbsoluteAtom *denormalize(IO &io) {
1174 ContextInfo *info = reinterpret_cast<ContextInfo*>(io.getContext());
1175 assert(info != nullptr);
1176 typedef MappingTraits<const lld::File*>::NormalizedFile NormalizedFile;
1177 NormalizedFile *f = reinterpret_cast<NormalizedFile*>(info->_currentFile);
1178 if ( !_name.empty() )
1179 _name = f->copyString(_name);
Shankar Easwaran8962feb2013-03-14 16:09:49 +00001180
1181 DEBUG_WITH_TYPE("WriterYAML", llvm::dbgs()
1182 << "created AbsoluteAtom named: '" << _name
1183 << "' (" << (void*)_name.data() << ", "
Nick Kledzik6b079f52013-01-05 02:22:35 +00001184 << _name.size() << ")\n");
1185 return this;
1186 }
1187 // Extract current File object from YAML I/O parsing context
1188 const lld::File &fileFromContext(IO &io) {
1189 ContextInfo *info = reinterpret_cast<ContextInfo*>(io.getContext());
1190 assert(info != nullptr);
1191 assert(info->_currentFile != nullptr);
1192 return *info->_currentFile;
1193 }
1194
1195 virtual const lld::File &file() const { return _file; }
1196 virtual StringRef name() const { return _name; }
1197 virtual uint64_t value() const { return _value; }
1198 virtual Scope scope() const { return _scope; }
Shankar Easwaran8962feb2013-03-14 16:09:49 +00001199
Nick Kledzik6b079f52013-01-05 02:22:35 +00001200 const lld::File &_file;
1201 StringRef _name;
1202 StringRef _refName;
1203 Scope _scope;
1204 Hex64 _value;
1205 };
1206
1207
1208 static void mapping(IO &io, const lld::AbsoluteAtom *&atom) {
Shankar Easwaran8962feb2013-03-14 16:09:49 +00001209 MappingNormalizationHeap<NormalizedAtom,
Nick Kledzik6b079f52013-01-05 02:22:35 +00001210 const lld::AbsoluteAtom*> keys(io, atom);
1211
1212 if ( io.outputting() ) {
1213 typedef MappingTraits<const lld::File*>::NormalizedFile NormalizedFile;
1214 ContextInfo *info = reinterpret_cast<ContextInfo*>(io.getContext());
1215 assert(info != nullptr);
1216 NormalizedFile *f = reinterpret_cast<NormalizedFile*>(info->_currentFile);
1217 assert(f);
1218 assert(f->_rnb);
1219 if ( f->_rnb->hasRefName(atom) ) {
1220 keys->_refName = f->_rnb->refName(atom);
1221 }
1222 }
1223
1224 io.mapRequired("name", keys->_name);
1225 io.mapOptional("ref-name", keys->_refName, StringRef());
1226 io.mapOptional("scope", keys->_scope);
1227 io.mapRequired("value", keys->_value);
1228 }
1229};
1230
Nick Kledzikbd491982013-01-08 23:51:03 +00001231} // namespace llvm
Shankar Easwaran8962feb2013-03-14 16:09:49 +00001232} // namespace yaml
Nick Kledzikbd491982013-01-08 23:51:03 +00001233
Nick Kledzik6b079f52013-01-05 02:22:35 +00001234
1235RefNameResolver::RefNameResolver(const lld::File *file, IO &io) : _io(io) {
1236 typedef MappingTraits<const lld::DefinedAtom*>::NormalizedAtom NormalizedAtom;
1237 for (const lld::DefinedAtom *a : file->defined() ) {
1238 NormalizedAtom *na = (NormalizedAtom*)a;
1239 if ( na->_refName.empty() )
1240 add(na->_name, a);
1241 else
1242 add(na->_refName, a);
1243 }
Shankar Easwaran8962feb2013-03-14 16:09:49 +00001244
Nick Kledzik6b079f52013-01-05 02:22:35 +00001245 for (const lld::UndefinedAtom *a : file->undefined() )
1246 add(a->name(), a);
Shankar Easwaran8962feb2013-03-14 16:09:49 +00001247
Nick Kledzik6b079f52013-01-05 02:22:35 +00001248 for (const lld::SharedLibraryAtom *a : file->sharedLibrary() )
1249 add(a->name(), a);
Shankar Easwaran8962feb2013-03-14 16:09:49 +00001250
Nick Kledzik6b079f52013-01-05 02:22:35 +00001251 typedef MappingTraits<const lld::AbsoluteAtom*>::NormalizedAtom NormAbsAtom;
1252 for (const lld::AbsoluteAtom *a : file->absolute() ) {
1253 NormAbsAtom *na = (NormAbsAtom*)a;
1254 if ( na->_refName.empty() )
1255 add(na->_name, a);
1256 else
1257 add(na->_refName, a);
1258 }
1259}
1260
1261
Nick Kledzikbd491982013-01-08 23:51:03 +00001262
Shankar Easwaran8962feb2013-03-14 16:09:49 +00001263inline
Nick Kledzikbd491982013-01-08 23:51:03 +00001264const lld::File*
1265MappingTraits<const lld::File*>::NormalizedFile::denormalize(IO &io) {
1266 typedef MappingTraits<const lld::DefinedAtom*>::NormalizedAtom NormalizedAtom;
Shankar Easwaran8962feb2013-03-14 16:09:49 +00001267
Nick Kledzikbd491982013-01-08 23:51:03 +00001268 RefNameResolver nameResolver(this, io);
1269 // Now that all atoms are parsed, references can be bound.
1270 for (const lld::DefinedAtom *a : this->defined() ) {
1271 NormalizedAtom *normAtom = (NormalizedAtom*)a;
1272 normAtom->bind(nameResolver);
1273 }
1274 return this;
1275}
1276
1277inline
1278void MappingTraits<const lld::DefinedAtom*>::
1279 NormalizedAtom::bind(const RefNameResolver &resolver) {
Shankar Easwaran8962feb2013-03-14 16:09:49 +00001280 typedef MappingTraits<const lld::Reference*>::NormalizedReference
Nick Kledzikbd491982013-01-08 23:51:03 +00001281 NormalizedReference;
1282 for (const lld::Reference *ref : _references) {
1283 NormalizedReference *normRef = (NormalizedReference*)ref;
1284 normRef->bind(resolver);
1285 }
1286}
1287
1288inline
1289void MappingTraits<const lld::Reference*>::
1290 NormalizedReference::bind(const RefNameResolver &resolver) {
1291 _target = resolver.lookup(_targetName);
1292}
1293
1294
Nick Kledzik6b079f52013-01-05 02:22:35 +00001295inline
1296llvm::StringRef MappingTraits<const lld::Reference*>::NormalizedReference::
1297 targetName(IO &io, const lld::Reference *ref) {
1298 if ( ref->target() == nullptr )
1299 return llvm::StringRef();
1300 ContextInfo *info = reinterpret_cast<ContextInfo*>(io.getContext());
1301 assert(info != nullptr);
1302 typedef MappingTraits<const lld::File*>::NormalizedFile NormalizedFile;
1303 NormalizedFile *f = reinterpret_cast<NormalizedFile*>(info->_currentFile);
1304 RefNameBuilder *rnb = f->_rnb;
Shankar Easwaran8962feb2013-03-14 16:09:49 +00001305 if ( rnb->hasRefName(ref->target()) )
Nick Kledzik6b079f52013-01-05 02:22:35 +00001306 return rnb->refName(ref->target());
1307 return ref->target()->name();
1308}
1309
1310
1311
1312namespace lld {
1313namespace yaml {
1314
1315class Writer : public lld::Writer {
1316public:
Michael J. Spencer64afcb42013-01-23 01:18:43 +00001317 Writer(const TargetInfo &ti) : _targetInfo(ti) {}
Shankar Easwaran8962feb2013-03-14 16:09:49 +00001318
Nick Kledzik6b079f52013-01-05 02:22:35 +00001319 virtual error_code writeFile(const lld::File &file, StringRef outPath) {
1320 // Create stream to path.
1321 std::string errorInfo;
1322 llvm::raw_fd_ostream out(outPath.data(), errorInfo);
1323 if (!errorInfo.empty())
1324 return llvm::make_error_code(llvm::errc::no_such_file_or_directory);
1325
1326 // Create yaml Output writer, using yaml options for context.
Michael J. Spencer64afcb42013-01-23 01:18:43 +00001327 ContextInfo context(_targetInfo);
Nick Kledzik6b079f52013-01-05 02:22:35 +00001328 llvm::yaml::Output yout(out, &context);
Shankar Easwaran8962feb2013-03-14 16:09:49 +00001329
Nick Kledzik6b079f52013-01-05 02:22:35 +00001330 // Write yaml output.
1331 const lld::File *fileRef = &file;
1332 yout << fileRef;
Shankar Easwaran8962feb2013-03-14 16:09:49 +00001333
Nick Kledzik6b079f52013-01-05 02:22:35 +00001334 return error_code::success();
1335 }
Shankar Easwaran8962feb2013-03-14 16:09:49 +00001336
Nick Kledzik6b079f52013-01-05 02:22:35 +00001337private:
Michael J. Spencer64afcb42013-01-23 01:18:43 +00001338 const TargetInfo &_targetInfo;
Nick Kledzik6b079f52013-01-05 02:22:35 +00001339};
1340
Nick Kledzik6b079f52013-01-05 02:22:35 +00001341class ReaderYAML : public Reader {
1342public:
Michael J. Spencer64afcb42013-01-23 01:18:43 +00001343 ReaderYAML(const TargetInfo &ti) : Reader(ti) {}
Nick Kledzik6b079f52013-01-05 02:22:35 +00001344
Michael J. Spencere6d56092013-04-05 22:04:44 +00001345 error_code parseFile(std::unique_ptr<MemoryBuffer> &mb,
Nick Kledzikc314b462013-04-04 18:59:24 +00001346 std::vector<std::unique_ptr<File>> &result) const {
Shankar Easwaran8962feb2013-03-14 16:09:49 +00001347 // Note: we do not take ownership of the MemoryBuffer. That is
Nick Kledzik6b079f52013-01-05 02:22:35 +00001348 // because yaml may produce multiple File objects, so there is no
1349 // *one* File to take ownership. Therefore, the yaml File objects
1350 // produced must make copies of all strings that come from YAML I/O.
1351 // Otherwise the strings will become invalid when this MemoryBuffer
1352 // is deallocated.
1353
1354 // Create YAML Input parser.
Michael J. Spencer64afcb42013-01-23 01:18:43 +00001355 ContextInfo context(_targetInfo);
Nick Kledzik6b079f52013-01-05 02:22:35 +00001356 llvm::yaml::Input yin(mb->getBuffer(), &context);
Shankar Easwaran8962feb2013-03-14 16:09:49 +00001357
Nick Kledzik6b079f52013-01-05 02:22:35 +00001358 // Fill vector with File objects created by parsing yaml.
1359 std::vector<const lld::File*> createdFiles;
1360 yin >> createdFiles;
Shankar Easwaran8962feb2013-03-14 16:09:49 +00001361
Nick Kledzik6b079f52013-01-05 02:22:35 +00001362 // Quit now if there were parsing errors.
1363 if ( yin.error() )
1364 return make_error_code(lld::yaml_reader_error::illegal_value);
Shankar Easwaran8962feb2013-03-14 16:09:49 +00001365
Nick Kledzik6b079f52013-01-05 02:22:35 +00001366 for (const File *file : createdFiles) {
Shankar Easwaran8962feb2013-03-14 16:09:49 +00001367 // Note: parseFile() should return vector of *const* File
Nick Kledzik6b079f52013-01-05 02:22:35 +00001368 File *f = const_cast<File*>(file);
1369 result.emplace_back(f);
1370 }
1371 return make_error_code(lld::yaml_reader_error::success);
1372 }
Nick Kledzik6b079f52013-01-05 02:22:35 +00001373};
Michael J. Spencer64afcb42013-01-23 01:18:43 +00001374} // end namespace yaml
Nick Kledzik6b079f52013-01-05 02:22:35 +00001375
Michael J. Spencer64afcb42013-01-23 01:18:43 +00001376std::unique_ptr<Writer> createWriterYAML(const TargetInfo &ti) {
1377 return std::unique_ptr<Writer>(new lld::yaml::Writer(ti));
Nick Kledzik6b079f52013-01-05 02:22:35 +00001378}
1379
Michael J. Spencer64afcb42013-01-23 01:18:43 +00001380std::unique_ptr<Reader> createReaderYAML(const TargetInfo &ti) {
1381 return std::unique_ptr<Reader>(new lld::yaml::ReaderYAML(ti));
Nick Kledzik6b079f52013-01-05 02:22:35 +00001382}
Michael J. Spencer64afcb42013-01-23 01:18:43 +00001383} // end namespace lld