blob: c2945f719f72995fffeabb325965e2a187245391 [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);
Michael J. Spencer64afcb42013-01-23 01:18:43 +0000291 auto relocStr = info->_targetInfo.stringFromRelocKind(value);
292 out << (relocStr ? *relocStr : "<unknown>");
Nick Kledzik6b079f52013-01-05 02:22:35 +0000293 }
294
295 static StringRef input(StringRef scalar, void *ctxt, RefKind &value) {
296 assert(ctxt != nullptr);
297 ContextInfo *info = reinterpret_cast<ContextInfo*>(ctxt);
Michael J. Spencer64afcb42013-01-23 01:18:43 +0000298 auto relocKind = info->_targetInfo.relocKindFromString(scalar);
299 if (!relocKind)
300 return "Invalid relocation kind";
301 value = *relocKind;
Nick Kledzik6b079f52013-01-05 02:22:35 +0000302 return StringRef();
303 }
304};
305
306
307template <>
308struct ScalarEnumerationTraits<lld::File::Kind> {
309 static void enumeration(IO &io, lld::File::Kind &value) {
310 io.enumCase(value, "object", lld::File::kindObject);
311 io.enumCase(value, "shared-library", lld::File::kindSharedLibrary);
312 io.enumCase(value, "static-library", lld::File::kindArchiveLibrary);
313 }
314};
315
316template <>
317struct ScalarEnumerationTraits<lld::Atom::Scope> {
318 static void enumeration(IO &io, lld::Atom::Scope &value) {
319 io.enumCase(value, "global", lld::Atom::scopeGlobal);
320 io.enumCase(value, "hidden", lld::Atom::scopeLinkageUnit);
321 io.enumCase(value, "static", lld::Atom::scopeTranslationUnit);
322 }
323};
324
325template <>
326struct ScalarEnumerationTraits<lld::DefinedAtom::SectionChoice> {
327 static void enumeration(IO &io, lld::DefinedAtom::SectionChoice &value) {
328 io.enumCase(value, "content", lld::DefinedAtom::sectionBasedOnContent);
329 io.enumCase(value, "custom", lld::DefinedAtom::sectionCustomPreferred);
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000330 io.enumCase(value, "custom-required",
Nick Kledzik6b079f52013-01-05 02:22:35 +0000331 lld::DefinedAtom::sectionCustomRequired);
332 }
333};
334
335template <>
Nick Kledzik36293f62013-01-23 22:32:56 +0000336struct ScalarEnumerationTraits<lld::DefinedAtom::SectionPosition> {
337 static void enumeration(IO &io, lld::DefinedAtom::SectionPosition &value) {
338 io.enumCase(value, "start", lld::DefinedAtom::sectionPositionStart);
339 io.enumCase(value, "early", lld::DefinedAtom::sectionPositionEarly);
340 io.enumCase(value, "any", lld::DefinedAtom::sectionPositionAny);
341 io.enumCase(value, "end", lld::DefinedAtom::sectionPositionEnd);
342 }
343};
344
345template <>
Nick Kledzik6b079f52013-01-05 02:22:35 +0000346struct ScalarEnumerationTraits<lld::DefinedAtom::Interposable> {
347 static void enumeration(IO &io, lld::DefinedAtom::Interposable &value) {
348 io.enumCase(value, "no", lld::DefinedAtom::interposeNo);
349 io.enumCase(value, "yes", lld::DefinedAtom::interposeYes);
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000350 io.enumCase(value, "yes-and-weak",
Nick Kledzik6b079f52013-01-05 02:22:35 +0000351 lld::DefinedAtom::interposeYesAndRuntimeWeak);
352 }
353};
354
355template <>
356struct ScalarEnumerationTraits<lld::DefinedAtom::Merge> {
357 static void enumeration(IO &io, lld::DefinedAtom::Merge &value) {
358 io.enumCase(value, "no", lld::DefinedAtom::mergeNo);
359 io.enumCase(value, "as-tentative", lld::DefinedAtom::mergeAsTentative);
360 io.enumCase(value, "as-weak", lld::DefinedAtom::mergeAsWeak);
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000361 io.enumCase(value, "as-addressed-weak",
Nick Kledzik6b079f52013-01-05 02:22:35 +0000362 lld::DefinedAtom::mergeAsWeakAndAddressUsed);
Nick Kledzik233f5372013-01-15 00:17:57 +0000363 io.enumCase(value, "by-content", lld::DefinedAtom::mergeByContent);
Nick Kledzik6b079f52013-01-05 02:22:35 +0000364 }
365};
366
367template <>
368struct ScalarEnumerationTraits<lld::DefinedAtom::DeadStripKind> {
369 static void enumeration(IO &io, lld::DefinedAtom::DeadStripKind &value) {
370 io.enumCase(value, "normal", lld::DefinedAtom::deadStripNormal);
371 io.enumCase(value, "never", lld::DefinedAtom::deadStripNever);
372 io.enumCase(value, "always", lld::DefinedAtom::deadStripAlways);
373 }
374};
375
376template <>
377struct ScalarEnumerationTraits<lld::DefinedAtom::ContentPermissions> {
378 static void enumeration(IO &io, lld::DefinedAtom::ContentPermissions &value) {
Nick Kledzikcc3d2dc2013-01-09 01:17:12 +0000379 io.enumCase(value, "---", lld::DefinedAtom::perm___);
380 io.enumCase(value, "r--", lld::DefinedAtom::permR__);
381 io.enumCase(value, "r-x", lld::DefinedAtom::permR_X);
382 io.enumCase(value, "rw-", lld::DefinedAtom::permRW_);
383 io.enumCase(value, "rwx", lld::DefinedAtom::permRWX);
384 io.enumCase(value, "rw-l", lld::DefinedAtom::permRW_L);
385 io.enumCase(value, "unknown", lld::DefinedAtom::permUnknown);
Nick Kledzik6b079f52013-01-05 02:22:35 +0000386 }
387};
388
389template <>
390struct ScalarEnumerationTraits<lld::DefinedAtom::ContentType> {
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000391 static void enumeration(IO &io, lld::DefinedAtom::ContentType &value) {
392 io.enumCase(value, "unknown",
Nick Kledzik6b079f52013-01-05 02:22:35 +0000393 lld::DefinedAtom::typeUnknown);
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000394 io.enumCase(value, "code",
Nick Kledzik6b079f52013-01-05 02:22:35 +0000395 lld::DefinedAtom::typeCode);
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000396 io.enumCase(value, "stub",
Nick Kledzik6b079f52013-01-05 02:22:35 +0000397 lld::DefinedAtom::typeStub);
Shankar Easwaran873c9ff2013-02-22 17:18:53 +0000398 io.enumCase(value, "constant", lld::DefinedAtom::typeConstant);
399 io.enumCase(value, "data", lld::DefinedAtom::typeData);
400 io.enumCase(value, "quick-data", lld::DefinedAtom::typeDataFast);
401 io.enumCase(value, "zero-fill", lld::DefinedAtom::typeZeroFill);
Shankar Easwarandb74ffb2013-02-24 03:09:10 +0000402 io.enumCase(value, "zero-fill-quick", lld::DefinedAtom::typeZeroFillFast);
Shankar Easwaran873c9ff2013-02-22 17:18:53 +0000403 io.enumCase(value, "const-data", lld::DefinedAtom::typeConstData);
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000404 io.enumCase(value, "got",
Nick Kledzik6b079f52013-01-05 02:22:35 +0000405 lld::DefinedAtom::typeGOT);
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000406 io.enumCase(value, "resolver",
Nick Kledzik6b079f52013-01-05 02:22:35 +0000407 lld::DefinedAtom::typeResolver);
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000408 io.enumCase(value, "branch-island",
Nick Kledzik6b079f52013-01-05 02:22:35 +0000409 lld::DefinedAtom::typeBranchIsland);
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000410 io.enumCase(value, "branch-shim",
Nick Kledzik6b079f52013-01-05 02:22:35 +0000411 lld::DefinedAtom::typeBranchShim);
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000412 io.enumCase(value, "stub-helper",
Nick Kledzik6b079f52013-01-05 02:22:35 +0000413 lld::DefinedAtom::typeStubHelper);
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000414 io.enumCase(value, "c-string",
Nick Kledzik6b079f52013-01-05 02:22:35 +0000415 lld::DefinedAtom::typeCString);
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000416 io.enumCase(value, "utf16-string",
Nick Kledzik6b079f52013-01-05 02:22:35 +0000417 lld::DefinedAtom::typeUTF16String);
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000418 io.enumCase(value, "unwind-cfi",
Nick Kledzik6b079f52013-01-05 02:22:35 +0000419 lld::DefinedAtom::typeCFI);
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000420 io.enumCase(value, "unwind-lsda",
Nick Kledzik6b079f52013-01-05 02:22:35 +0000421 lld::DefinedAtom::typeLSDA);
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000422 io.enumCase(value, "const-4-byte",
Nick Kledzik6b079f52013-01-05 02:22:35 +0000423 lld::DefinedAtom::typeLiteral4);
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000424 io.enumCase(value, "const-8-byte",
Nick Kledzik6b079f52013-01-05 02:22:35 +0000425 lld::DefinedAtom::typeLiteral8);
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000426 io.enumCase(value, "const-16-byte",
Nick Kledzik6b079f52013-01-05 02:22:35 +0000427 lld::DefinedAtom::typeLiteral16);
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000428 io.enumCase(value, "lazy-pointer",
Nick Kledzik6b079f52013-01-05 02:22:35 +0000429 lld::DefinedAtom::typeLazyPointer);
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000430 io.enumCase(value, "lazy-dylib-pointer",
Nick Kledzik6b079f52013-01-05 02:22:35 +0000431 lld::DefinedAtom::typeLazyDylibPointer);
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000432 io.enumCase(value, "cfstring",
Nick Kledzik6b079f52013-01-05 02:22:35 +0000433 lld::DefinedAtom::typeCFString);
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000434 io.enumCase(value, "initializer-pointer",
Nick Kledzik6b079f52013-01-05 02:22:35 +0000435 lld::DefinedAtom::typeInitializerPtr);
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000436 io.enumCase(value, "terminator-pointer",
Nick Kledzik6b079f52013-01-05 02:22:35 +0000437 lld::DefinedAtom::typeTerminatorPtr);
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000438 io.enumCase(value, "c-string-pointer",
Nick Kledzik6b079f52013-01-05 02:22:35 +0000439 lld::DefinedAtom::typeCStringPtr);
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000440 io.enumCase(value, "objc-class-pointer",
Nick Kledzik6b079f52013-01-05 02:22:35 +0000441 lld::DefinedAtom::typeObjCClassPtr);
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000442 io.enumCase(value, "objc-category-list",
Nick Kledzik6b079f52013-01-05 02:22:35 +0000443 lld::DefinedAtom::typeObjC2CategoryList);
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000444 io.enumCase(value, "objc-class1",
Nick Kledzik6b079f52013-01-05 02:22:35 +0000445 lld::DefinedAtom::typeObjC1Class);
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000446 io.enumCase(value, "dtraceDOF",
Nick Kledzik6b079f52013-01-05 02:22:35 +0000447 lld::DefinedAtom::typeDTraceDOF);
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000448 io.enumCase(value, "lto-temp",
Nick Kledzik6b079f52013-01-05 02:22:35 +0000449 lld::DefinedAtom::typeTempLTO);
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000450 io.enumCase(value, "compact-unwind",
Nick Kledzik6b079f52013-01-05 02:22:35 +0000451 lld::DefinedAtom::typeCompactUnwindInfo);
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000452 io.enumCase(value, "tlv-thunk",
Nick Kledzik6b079f52013-01-05 02:22:35 +0000453 lld::DefinedAtom::typeThunkTLV);
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000454 io.enumCase(value, "tlv-data",
Nick Kledzik6b079f52013-01-05 02:22:35 +0000455 lld::DefinedAtom::typeTLVInitialData);
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000456 io.enumCase(value, "tlv-zero-fill",
Nick Kledzik6b079f52013-01-05 02:22:35 +0000457 lld::DefinedAtom::typeTLVInitialZeroFill);
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000458 io.enumCase(value, "tlv-initializer-ptr",
Nick Kledzik6b079f52013-01-05 02:22:35 +0000459 lld::DefinedAtom::typeTLVInitializerPtr);
Nick Kledzik6b079f52013-01-05 02:22:35 +0000460 }
461};
462
463template <>
464struct ScalarEnumerationTraits<lld::UndefinedAtom::CanBeNull> {
465 static void enumeration(IO &io, lld::UndefinedAtom::CanBeNull &value) {
466 io.enumCase(value, "never", lld::UndefinedAtom::canBeNullNever);
467 io.enumCase(value, "at-runtime", lld::UndefinedAtom::canBeNullAtRuntime);
468 io.enumCase(value, "at-buildtime", lld::UndefinedAtom::canBeNullAtBuildtime);
469 }
470};
471
472
473template <>
474struct ScalarEnumerationTraits<ShlibCanBeNull> {
475 static void enumeration(IO &io, ShlibCanBeNull &value) {
476 io.enumCase(value, "never", false);
477 io.enumCase(value, "at-runtime", true);
478 }
479};
480
481
482
483/// This is a custom formatter for lld::DefinedAtom::Alignment. Values look
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000484/// like:
Nick Kledzik6b079f52013-01-05 02:22:35 +0000485/// 2^3 # 8-byte aligned
486/// 7 mod 2^4 # 16-byte aligned plus 7 bytes
487template<>
488struct ScalarTraits<lld::DefinedAtom::Alignment> {
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000489 static void output(const lld::DefinedAtom::Alignment &value, void *ctxt,
Nick Kledzik6b079f52013-01-05 02:22:35 +0000490 llvm::raw_ostream &out) {
491 if (value.modulus == 0) {
492 out << llvm::format("2^%d", value.powerOf2);
493 }
494 else {
495 out << llvm::format("%d mod 2^%d", value.modulus, value.powerOf2);
496 }
497 }
498
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000499 static StringRef input(StringRef scalar, void *ctxt,
Nick Kledzik6b079f52013-01-05 02:22:35 +0000500 lld::DefinedAtom::Alignment &value) {
501 value.modulus = 0;
502 size_t modStart = scalar.find("mod");
503 if (modStart != StringRef::npos) {
504 StringRef modStr = scalar.slice(0, modStart);
505 modStr = modStr.rtrim();
506 unsigned int modulus;
507 if (modStr.getAsInteger(0, modulus)) {
508 return "malformed alignment modulus";
509 }
510 value.modulus = modulus;
511 scalar = scalar.drop_front(modStart+3);
512 scalar = scalar.ltrim();
513 }
514 if (!scalar.startswith("2^")) {
515 return "malformed alignment";
516 }
517 StringRef powerStr = scalar.drop_front(2);
518 unsigned int power;
519 if (powerStr.getAsInteger(0, power)) {
520 return "malformed alignment power";
521 }
522 value.powerOf2 = power;
523 if (value.modulus > (1<<value.powerOf2)) {
524 return "malformed alignment, modulus too large for power";
525 }
526 return StringRef(); // returning empty string means success
527 }
528};
529
530
531
532
533template <>
534struct ScalarEnumerationTraits<FileKinds> {
535 static void enumeration(IO &io, FileKinds &value) {
536 io.enumCase(value, "object", fileKindObjectAtoms);
537 io.enumCase(value, "archive", fileKindArchive);
538 io.enumCase(value, "object-elf", fileKindObjectELF);
539 io.enumCase(value, "object-mach-o", fileKindObjectMachO);
540 }
541};
542
543template <>
544struct MappingTraits<ArchMember> {
545 static void mapping(IO &io, ArchMember &member) {
546 io.mapOptional("kind", member._kind, fileKindObjectAtoms);
547 io.mapOptional("name", member._name);
548 io.mapRequired("content", member._content);
549 }
550};
551
Nick Kledzik6b079f52013-01-05 02:22:35 +0000552
553
554// Declare that an AtomList is a yaml sequence.
555template<typename T>
556struct SequenceTraits<AtomList<T>> {
557 static size_t size(IO &io, AtomList<T> &seq) {
558 return seq._atoms.size();
559 }
560 static const T *&element(IO &io, AtomList<T> &seq, size_t index) {
561 if (index >= seq._atoms.size())
562 seq._atoms.resize(index+1);
563 return seq._atoms[index];
564 }
565};
566
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000567// Used to allow DefinedAtom content bytes to be a flow sequence of
Nick Kledzik6b079f52013-01-05 02:22:35 +0000568// two-digit hex numbers without the leading 0x (e.g. FF, 04, 0A)
569template<>
570struct ScalarTraits<ImplicitHex8> {
571 static void output(const ImplicitHex8 &val, void*, llvm::raw_ostream &out) {
572 uint8_t num = val;
573 out << llvm::format("%02X", num);
574 }
575
576 static llvm::StringRef input(llvm::StringRef str, void*, ImplicitHex8 &val) {
577 unsigned long long n;
578 if (getAsUnsignedInteger(str, 16, n))
579 return "invalid two-digit-hex number";
580 if (n > 0xFF)
581 return "out of range two-digit-hex number";
582 val = n;
583 return StringRef(); // returning empty string means success
584 }
585};
586
Nick Kledzik6b079f52013-01-05 02:22:35 +0000587
588// YAML conversion for std::vector<const lld::File*>
589template<>
590struct DocumentListTraits< std::vector<const lld::File*> > {
591 static size_t size(IO &io, std::vector<const lld::File*> &seq) {
592 return seq.size();
593 }
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000594 static const lld::File *&element(IO &io, std::vector<const lld::File*> &seq,
Nick Kledzik6b079f52013-01-05 02:22:35 +0000595 size_t index) {
596 if (index >= seq.size())
597 seq.resize(index+1);
598 return seq[index];
599 }
600};
601
602
603// YAML conversion for const lld::File*
604template <>
605struct MappingTraits<const lld::File*> {
Shankar Easwaran3256d4f2013-01-25 07:39:18 +0000606
607 class NormArchiveFile : public lld::ArchiveLibraryFile {
608 public:
609 NormArchiveFile(IO &io)
610 : ArchiveLibraryFile(((ContextInfo *)io.getContext())->_targetInfo,
611 ""),
612 _path() {
613 }
614 NormArchiveFile(IO &io, const lld::File *file)
615 : ArchiveLibraryFile(((ContextInfo *)io.getContext())->_targetInfo,
616 file->path()),
617 _path(file->path()) {
Nick Kledzik6b079f52013-01-05 02:22:35 +0000618 // If we want to support writing archives, this constructor would
619 // need to populate _members.
Shankar Easwaran3256d4f2013-01-25 07:39:18 +0000620 }
Nick Kledzik6b079f52013-01-05 02:22:35 +0000621
622 const lld::File *denormalize(IO &io) {
623 return this;
624 }
Michael J. Spencer57752dc2013-01-12 02:45:54 +0000625
Nick Kledzik36293f62013-01-23 22:32:56 +0000626 virtual void setOrdinalAndIncrement(uint64_t &ordinal) const {
627 _ordinal = ordinal++;
628 // Assign sequential ordinals to member files
629 for (const ArchMember &member : _members) {
630 member._content->setOrdinalAndIncrement(ordinal);
631 }
632 }
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000633
Nick Kledzik6b079f52013-01-05 02:22:35 +0000634 virtual const atom_collection<lld::DefinedAtom> &defined() const {
635 return _noDefinedAtoms;
636 }
637 virtual const atom_collection<lld::UndefinedAtom> &undefined() const {
638 return _noUndefinedAtoms;
639 }
640 virtual const atom_collection<lld::SharedLibraryAtom> &sharedLibrary()const{
641 return _noSharedLibaryAtoms;
642 }
643 virtual const atom_collection<lld::AbsoluteAtom> &absolute() const {
644 return _noAbsoluteAtoms;
645 }
646 virtual const File *find(StringRef name, bool dataSymbolOnly) const {
647 for (const ArchMember &member : _members) {
648 for (const lld::DefinedAtom *atom : member._content->defined() ) {
649 if (name == atom->name()) {
650 if (!dataSymbolOnly)
651 return member._content;
652 switch (atom->contentType()) {
653 case lld::DefinedAtom::typeData:
654 case lld::DefinedAtom::typeZeroFill:
655 return member._content;
656 default:
657 break;
658 }
659 }
660 }
661 }
662 return nullptr;
663 }
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000664
Nick Kledzik6b079f52013-01-05 02:22:35 +0000665 StringRef _path;
666 std::vector<ArchMember> _members;
667 };
Shankar Easwaran3256d4f2013-01-25 07:39:18 +0000668
669 class NormalizedFile : public lld::File {
670 public:
Michael J. Spencer0f3dd612013-03-20 18:57:27 +0000671 NormalizedFile(IO &io) : File("", kindObject), _IO(io), _rnb(nullptr) {}
Shankar Easwaran3256d4f2013-01-25 07:39:18 +0000672 NormalizedFile(IO &io, const lld::File *file)
Michael J. Spencer0f3dd612013-03-20 18:57:27 +0000673 : File(file->path(), kindObject), _IO(io), _rnb(new RefNameBuilder(*file)),
Shankar Easwaran3256d4f2013-01-25 07:39:18 +0000674 _path(file->path()) {
675 for (const lld::DefinedAtom *a : file->defined())
676 _definedAtoms.push_back(a);
677 for (const lld::UndefinedAtom *a : file->undefined())
Nick Kledzik6b079f52013-01-05 02:22:35 +0000678 _undefinedAtoms.push_back(a);
679 for (const lld::SharedLibraryAtom *a : file->sharedLibrary())
680 _sharedLibraryAtoms.push_back(a);
681 for (const lld::AbsoluteAtom *a : file->absolute())
682 _absoluteAtoms.push_back(a);
683 }
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000684 const lld::File *denormalize(IO &io);
Michael J. Spencer57752dc2013-01-12 02:45:54 +0000685
Nick Kledzik6b079f52013-01-05 02:22:35 +0000686 virtual const atom_collection<lld::DefinedAtom> &defined() const {
687 return _definedAtoms;
688 }
689 virtual const atom_collection<lld::UndefinedAtom> &undefined() const {
690 return _undefinedAtoms;
691 }
692 virtual const atom_collection<lld::SharedLibraryAtom> &sharedLibrary()const{
693 return _sharedLibraryAtoms;
694 }
Shankar Easwaran3256d4f2013-01-25 07:39:18 +0000695 virtual const atom_collection<lld::AbsoluteAtom> &absolute() const {
696 return _absoluteAtoms;
697 }
698
699 virtual const TargetInfo &getTargetInfo() const {
700 return ((ContextInfo *)_IO.getContext())->_targetInfo;
701 }
702
703 // Allocate a new copy of this string and keep track of allocations
704 // in _stringCopies, so they can be freed when File is destroyed.
Nick Kledzik6b079f52013-01-05 02:22:35 +0000705 StringRef copyString(StringRef str) {
706 // We want _stringCopies to own the string memory so it is deallocated
707 // when the File object is destroyed. But we need a StringRef that
708 // points into that memory.
Michael J. Spencer20231f12013-01-26 12:26:56 +0000709 std::unique_ptr<char[]> s(new char[str.size()]);
Nick Kledzik6b079f52013-01-05 02:22:35 +0000710 memcpy(s.get(), str.data(), str.size());
711 llvm::StringRef r = llvm::StringRef(s.get(), str.size());
712 _stringCopies.push_back(std::move(s));
Shankar Easwaran3256d4f2013-01-25 07:39:18 +0000713 return r;
714 }
715
716 IO &_IO;
717 RefNameBuilder *_rnb;
718 StringRef _path;
719 AtomList<lld::DefinedAtom> _definedAtoms;
Nick Kledzik6b079f52013-01-05 02:22:35 +0000720 AtomList<lld::UndefinedAtom> _undefinedAtoms;
721 AtomList<lld::SharedLibraryAtom> _sharedLibraryAtoms;
722 AtomList<lld::AbsoluteAtom> _absoluteAtoms;
Michael J. Spencer20231f12013-01-26 12:26:56 +0000723 std::vector<std::unique_ptr<char[]>> _stringCopies;
Nick Kledzik6b079f52013-01-05 02:22:35 +0000724 };
725
726
727 static void mapping(IO &io, const lld::File *&file) {
728 // We only support writing atom based YAML
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000729 FileKinds kind = fileKindObjectAtoms;
Nick Kledzik6b079f52013-01-05 02:22:35 +0000730 // If reading, peek ahead to see what kind of file this is.
731 io.mapOptional("kind", kind, fileKindObjectAtoms);
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000732 //
Nick Kledzik6b079f52013-01-05 02:22:35 +0000733 switch (kind) {
734 case fileKindObjectAtoms:
735 mappingAtoms(io, file);
736 break;
737 case fileKindArchive:
738 mappingArchive(io, file);
739 break;
740 case fileKindObjectELF:
741 case fileKindObjectMachO:
742 // Eventually we will have an external function to call, similar
743 // to mappingAtoms() and mappingArchive() but implememented
744 // with coresponding file format code.
Nick Kledzik6b079f52013-01-05 02:22:35 +0000745 llvm_unreachable("section based YAML not supported yet");
746 }
747 }
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000748
Nick Kledzik6b079f52013-01-05 02:22:35 +0000749 static void mappingAtoms(IO &io, const lld::File *&file) {
750 MappingNormalizationHeap<NormalizedFile, const lld::File*> keys(io, file);
751 ContextInfo *info = reinterpret_cast<ContextInfo*>(io.getContext());
752 assert(info != nullptr);
753 info->_currentFile = keys.operator->();
754
755 io.mapOptional("path", keys->_path);
756 io.mapOptional("defined-atoms", keys->_definedAtoms);
757 io.mapOptional("undefined-atoms", keys->_undefinedAtoms);
758 io.mapOptional("shared-library-atoms", keys->_sharedLibraryAtoms);
759 io.mapOptional("absolute-atoms", keys->_absoluteAtoms);
760 }
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000761
Nick Kledzik6b079f52013-01-05 02:22:35 +0000762 static void mappingArchive(IO &io, const lld::File *&file) {
763 MappingNormalizationHeap<NormArchiveFile, const lld::File*> keys(io, file);
764
765 io.mapOptional("path", keys->_path);
766 io.mapOptional("members", keys->_members);
767 }
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000768
Nick Kledzik6b079f52013-01-05 02:22:35 +0000769};
770
771
772
773// YAML conversion for const lld::Reference*
774template <>
775struct MappingTraits<const lld::Reference*> {
776
777 class NormalizedReference : public lld::Reference {
778 public:
779 NormalizedReference(IO &io)
Michael J. Spencerfa405272013-03-20 18:57:52 +0000780 : _target(nullptr), _targetName(), _offset(0), _addend(0) {}
781
Nick Kledzik6b079f52013-01-05 02:22:35 +0000782 NormalizedReference(IO &io, const lld::Reference *ref)
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000783 : _target(nullptr),
784 _targetName(targetName(io, ref)),
785 _offset(ref->offsetInAtom()),
Nick Kledzik6b079f52013-01-05 02:22:35 +0000786 _addend(ref->addend()),
Michael J. Spencerfa405272013-03-20 18:57:52 +0000787 _mappedKind(ref->kind()) {
Nick Kledzik6b079f52013-01-05 02:22:35 +0000788 }
Michael J. Spencerfa405272013-03-20 18:57:52 +0000789
Nick Kledzik6b079f52013-01-05 02:22:35 +0000790 const lld::Reference *denormalize(IO &io) {
791 ContextInfo *info = reinterpret_cast<ContextInfo*>(io.getContext());
792 assert(info != nullptr);
793 typedef MappingTraits<const lld::File*>::NormalizedFile NormalizedFile;
794 NormalizedFile *f = reinterpret_cast<NormalizedFile*>(info->_currentFile);
795 if (!_targetName.empty())
796 _targetName = f->copyString(_targetName);
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000797 DEBUG_WITH_TYPE("WriterYAML", llvm::dbgs()
798 << "created Reference to name: '" << _targetName
799 << "' (" << (void*)_targetName.data() << ", "
Nick Kledzik6b079f52013-01-05 02:22:35 +0000800 << _targetName.size() << ")\n");
Michael J. Spencerfa405272013-03-20 18:57:52 +0000801 setKind(_mappedKind);
Nick Kledzik6b079f52013-01-05 02:22:35 +0000802 return this;
803 }
804 void bind(const RefNameResolver&);
805 static StringRef targetName(IO &io, const lld::Reference *ref);
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000806
Nick Kledzik6b079f52013-01-05 02:22:35 +0000807 virtual uint64_t offsetInAtom() const { return _offset; }
Nick Kledzik6b079f52013-01-05 02:22:35 +0000808 virtual const lld::Atom *target() const { return _target; }
809 virtual Addend addend() const { return _addend; }
Nick Kledzik6b079f52013-01-05 02:22:35 +0000810 virtual void setAddend(Addend a) { _addend = a; }
811 virtual void setTarget(const lld::Atom *a) { _target = a; }
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000812
Nick Kledzik6b079f52013-01-05 02:22:35 +0000813 const lld::Atom *_target;
814 StringRef _targetName;
815 uint32_t _offset;
816 Addend _addend;
Michael J. Spencerfa405272013-03-20 18:57:52 +0000817 RefKind _mappedKind;
Nick Kledzik6b079f52013-01-05 02:22:35 +0000818 };
819
820
821 static void mapping(IO &io, const lld::Reference *&ref) {
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000822 MappingNormalizationHeap<NormalizedReference,
Nick Kledzik6b079f52013-01-05 02:22:35 +0000823 const lld::Reference*> keys(io, ref);
824
Michael J. Spencerfa405272013-03-20 18:57:52 +0000825 io.mapRequired("kind", keys->_mappedKind);
Nick Kledzik6b079f52013-01-05 02:22:35 +0000826 io.mapOptional("offset", keys->_offset);
827 io.mapOptional("target", keys->_targetName);
828 io.mapOptional("addend", keys->_addend, (lld::Reference::Addend)0);
829 }
830};
831
Nick Kledzik6b079f52013-01-05 02:22:35 +0000832
833
834// YAML conversion for const lld::DefinedAtom*
835template <>
836struct MappingTraits<const lld::DefinedAtom*> {
837
838 class NormalizedAtom : public lld::DefinedAtom {
839 public:
840 NormalizedAtom(IO &io)
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000841 : _file(fileFromContext(io)), _name(), _refName(),
Nick Kledzik6b079f52013-01-05 02:22:35 +0000842 _alignment(0), _content(), _references() {
Nick Kledzik36293f62013-01-23 22:32:56 +0000843 static uint32_t ordinalCounter = 1;
844 _ordinal = ordinalCounter++;
Nick Kledzik6b079f52013-01-05 02:22:35 +0000845 }
846 NormalizedAtom(IO &io, const lld::DefinedAtom *atom)
847 : _file(fileFromContext(io)),
848 _name(atom->name()),
849 _refName(),
850 _scope(atom->scope()),
851 _interpose(atom->interposable()),
852 _merge(atom->merge()),
853 _contentType(atom->contentType()),
854 _alignment(atom->alignment()),
855 _sectionChoice(atom->sectionChoice()),
Nick Kledzik36293f62013-01-23 22:32:56 +0000856 _sectionPosition(atom->sectionPosition()),
Nick Kledzik6b079f52013-01-05 02:22:35 +0000857 _deadStrip(atom->deadStrip()),
858 _permissions(atom->permissions()),
859 _size(atom->size()),
860 _sectionName(atom->customSectionName()) {
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000861 for ( const lld::Reference *r : *atom )
Nick Kledzik6b079f52013-01-05 02:22:35 +0000862 _references.push_back(r);
863 ArrayRef<uint8_t> cont = atom->rawContent();
864 _content.reserve(cont.size());
865 for (uint8_t x : cont)
866 _content.push_back(x);
867 }
868 const lld::DefinedAtom *denormalize(IO &io) {
869 ContextInfo *info = reinterpret_cast<ContextInfo*>(io.getContext());
870 assert(info != nullptr);
871 typedef MappingTraits<const lld::File*>::NormalizedFile NormalizedFile;
872 NormalizedFile *f = reinterpret_cast<NormalizedFile*>(info->_currentFile);
873 if ( !_name.empty() )
874 _name = f->copyString(_name);
875 if ( !_refName.empty() )
876 _refName = f->copyString(_refName);
877 if ( !_sectionName.empty() )
878 _sectionName = f->copyString(_sectionName);
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000879 DEBUG_WITH_TYPE("WriterYAML", llvm::dbgs()
880 << "created DefinedAtom named: '" << _name
881 << "' (" << (void*)_name.data() << ", "
Nick Kledzik6b079f52013-01-05 02:22:35 +0000882 << _name.size() << ")\n");
883 return this;
884 }
885 void bind(const RefNameResolver&);
886 // Extract current File object from YAML I/O parsing context
887 const lld::File &fileFromContext(IO &io) {
888 ContextInfo *info = reinterpret_cast<ContextInfo*>(io.getContext());
889 assert(info != nullptr);
890 assert(info->_currentFile != nullptr);
891 return *info->_currentFile;
892 }
893
894 virtual const lld::File &file() const { return _file; }
895 virtual StringRef name() const { return _name; }
896 virtual uint64_t size() const { return _size; }
897 virtual Scope scope() const { return _scope; }
898 virtual Interposable interposable() const { return _interpose; }
899 virtual Merge merge() const { return _merge; }
900 virtual ContentType contentType() const { return _contentType; }
901 virtual Alignment alignment() const { return _alignment; }
902 virtual SectionChoice sectionChoice() const { return _sectionChoice; }
903 virtual StringRef customSectionName() const { return _sectionName;}
Nick Kledzik36293f62013-01-23 22:32:56 +0000904 virtual SectionPosition sectionPosition() const{return _sectionPosition;}
Nick Kledzik6b079f52013-01-05 02:22:35 +0000905 virtual DeadStripKind deadStrip() const { return _deadStrip; }
906 virtual ContentPermissions permissions() const { return _permissions; }
907 virtual bool isThumb() const { return false; }
908 virtual bool isAlias() const { return false; }
Michael J. Spencer74ba7222013-01-13 01:09:39 +0000909 ArrayRef<uint8_t> rawContent() const {
910 return ArrayRef<uint8_t>(
911 reinterpret_cast<const uint8_t *>(_content.data()), _content.size());
912 }
913
Nick Kledzik36293f62013-01-23 22:32:56 +0000914 virtual uint64_t ordinal() const { return _ordinal; }
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000915
916 reference_iterator begin() const {
Nick Kledzik6b079f52013-01-05 02:22:35 +0000917 uintptr_t index = 0;
918 const void *it = reinterpret_cast<const void*>(index);
919 return reference_iterator(*this, it);
920 }
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000921 reference_iterator end() const {
Nick Kledzik6b079f52013-01-05 02:22:35 +0000922 uintptr_t index = _references.size();
923 const void *it = reinterpret_cast<const void*>(index);
924 return reference_iterator(*this, it);
925 }
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000926 const lld::Reference *derefIterator(const void *it) const {
Nick Kledzik6b079f52013-01-05 02:22:35 +0000927 uintptr_t index = reinterpret_cast<uintptr_t>(it);
928 assert(index < _references.size());
929 return _references[index];
930 }
931 void incrementIterator(const void *&it) const {
932 uintptr_t index = reinterpret_cast<uintptr_t>(it);
933 ++index;
934 it = reinterpret_cast<const void*>(index);
935 }
936
937 const lld::File &_file;
938 StringRef _name;
939 StringRef _refName;
940 Scope _scope;
941 Interposable _interpose;
942 Merge _merge;
943 ContentType _contentType;
944 Alignment _alignment;
945 SectionChoice _sectionChoice;
Nick Kledzik36293f62013-01-23 22:32:56 +0000946 SectionPosition _sectionPosition;
Nick Kledzik6b079f52013-01-05 02:22:35 +0000947 DeadStripKind _deadStrip;
948 ContentPermissions _permissions;
Nick Kledzik36293f62013-01-23 22:32:56 +0000949 uint32_t _ordinal;
Nick Kledzik6b079f52013-01-05 02:22:35 +0000950 std::vector<ImplicitHex8> _content;
951 uint64_t _size;
952 StringRef _sectionName;
953 std::vector<const lld::Reference*> _references;
954 };
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000955
Nick Kledzik6b079f52013-01-05 02:22:35 +0000956 static void mapping(IO &io, const lld::DefinedAtom *&atom) {
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000957 MappingNormalizationHeap<NormalizedAtom,
Nick Kledzik6b079f52013-01-05 02:22:35 +0000958 const lld::DefinedAtom*> keys(io, atom);
959 if ( io.outputting() ) {
960 // If writing YAML, check if atom needs a ref-name.
961 typedef MappingTraits<const lld::File*>::NormalizedFile NormalizedFile;
962 ContextInfo *info = reinterpret_cast<ContextInfo*>(io.getContext());
963 assert(info != nullptr);
964 NormalizedFile *f = reinterpret_cast<NormalizedFile*>(info->_currentFile);
965 assert(f);
966 assert(f->_rnb);
967 if ( f->_rnb->hasRefName(atom) ) {
968 keys->_refName = f->_rnb->refName(atom);
969 }
970 }
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000971
972 io.mapOptional("name", keys->_name,
Nick Kledzik6b079f52013-01-05 02:22:35 +0000973 StringRef());
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000974 io.mapOptional("ref-name", keys->_refName,
Nick Kledzik6b079f52013-01-05 02:22:35 +0000975 StringRef());
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000976 io.mapOptional("scope", keys->_scope,
Nick Kledzik6b079f52013-01-05 02:22:35 +0000977 lld::DefinedAtom::scopeTranslationUnit);
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000978 io.mapOptional("type", keys->_contentType,
Nick Kledzik6b079f52013-01-05 02:22:35 +0000979 lld::DefinedAtom::typeCode);
980 io.mapOptional("content", keys->_content);
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000981 io.mapOptional("size", keys->_size,
Nick Kledzik6b079f52013-01-05 02:22:35 +0000982 (uint64_t)keys->_content.size());
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000983 io.mapOptional("interposable", keys->_interpose,
Nick Kledzik6b079f52013-01-05 02:22:35 +0000984 lld::DefinedAtom::interposeNo);
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000985 io.mapOptional("merge", keys->_merge,
Nick Kledzik6b079f52013-01-05 02:22:35 +0000986 lld::DefinedAtom::mergeNo);
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000987 io.mapOptional("alignment", keys->_alignment,
Nick Kledzik6b079f52013-01-05 02:22:35 +0000988 lld::DefinedAtom::Alignment(0));
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000989 io.mapOptional("section-choice", keys->_sectionChoice,
Nick Kledzik6b079f52013-01-05 02:22:35 +0000990 lld::DefinedAtom::sectionBasedOnContent);
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000991 io.mapOptional("section-name", keys->_sectionName,
Nick Kledzik6b079f52013-01-05 02:22:35 +0000992 StringRef());
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000993 io.mapOptional("section-position",keys->_sectionPosition,
Nick Kledzik36293f62013-01-23 22:32:56 +0000994 lld::DefinedAtom::sectionPositionAny);
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000995 io.mapOptional("dead-strip", keys->_deadStrip,
Nick Kledzik6b079f52013-01-05 02:22:35 +0000996 lld::DefinedAtom::deadStripNormal);
Nick Kledzikcc3d2dc2013-01-09 01:17:12 +0000997 // default permissions based on content type
Shankar Easwaran8962feb2013-03-14 16:09:49 +0000998 io.mapOptional("permissions", keys->_permissions,
Nick Kledzikcc3d2dc2013-01-09 01:17:12 +0000999 lld::DefinedAtom::permissions(
1000 keys->_contentType));
Nick Kledzik8a3052e2013-01-08 21:12:13 +00001001 io.mapOptional("references", keys->_references);
Nick Kledzik6b079f52013-01-05 02:22:35 +00001002 }
1003};
1004
1005
1006
1007
Nick Kledzik6b079f52013-01-05 02:22:35 +00001008// YAML conversion for const lld::UndefinedAtom*
1009template <>
1010struct MappingTraits<const lld::UndefinedAtom*> {
1011
1012 class NormalizedAtom : public lld::UndefinedAtom {
1013 public:
1014 NormalizedAtom(IO &io)
1015 : _file(fileFromContext(io)), _name(), _canBeNull(canBeNullNever) {
1016 }
1017 NormalizedAtom(IO &io, const lld::UndefinedAtom *atom)
Shankar Easwaran8962feb2013-03-14 16:09:49 +00001018 : _file(fileFromContext(io)),
1019 _name(atom->name()),
Nick Kledzik6b079f52013-01-05 02:22:35 +00001020 _canBeNull(atom->canBeNull()) {
1021 }
1022 const lld::UndefinedAtom *denormalize(IO &io) {
1023 ContextInfo *info = reinterpret_cast<ContextInfo*>(io.getContext());
1024 assert(info != nullptr);
1025 typedef MappingTraits<const lld::File*>::NormalizedFile NormalizedFile;
1026 NormalizedFile *f = reinterpret_cast<NormalizedFile*>(info->_currentFile);
1027 if ( !_name.empty() )
1028 _name = f->copyString(_name);
1029
Shankar Easwaran8962feb2013-03-14 16:09:49 +00001030 DEBUG_WITH_TYPE("WriterYAML", llvm::dbgs()
1031 << "created UndefinedAtom named: '" << _name
1032 << "' (" << (void*)_name.data() << ", "
Nick Kledzik6b079f52013-01-05 02:22:35 +00001033 << _name.size() << ")\n");
1034 return this;
1035 }
1036 // Extract current File object from YAML I/O parsing context
1037 const lld::File &fileFromContext(IO &io) {
1038 ContextInfo *info = reinterpret_cast<ContextInfo*>(io.getContext());
1039 assert(info != nullptr);
1040 assert(info->_currentFile != nullptr);
1041 return *info->_currentFile;
1042 }
1043
1044 virtual const lld::File &file() const { return _file; }
1045 virtual StringRef name() const { return _name; }
1046 virtual CanBeNull canBeNull() const { return _canBeNull; }
Shankar Easwaran8962feb2013-03-14 16:09:49 +00001047
Nick Kledzik6b079f52013-01-05 02:22:35 +00001048 const lld::File &_file;
1049 StringRef _name;
1050 CanBeNull _canBeNull;
1051 };
1052
1053
1054 static void mapping(IO &io, const lld::UndefinedAtom* &atom) {
Shankar Easwaran8962feb2013-03-14 16:09:49 +00001055 MappingNormalizationHeap<NormalizedAtom,
Nick Kledzik6b079f52013-01-05 02:22:35 +00001056 const lld::UndefinedAtom*> keys(io, atom);
1057
1058 io.mapRequired("name", keys->_name);
Shankar Easwaran8962feb2013-03-14 16:09:49 +00001059 io.mapOptional("can-be-null", keys->_canBeNull,
Nick Kledzik6b079f52013-01-05 02:22:35 +00001060 lld::UndefinedAtom::canBeNullNever);
1061 }
1062};
1063
1064
Nick Kledzik36293f62013-01-23 22:32:56 +00001065
Nick Kledzik6b079f52013-01-05 02:22:35 +00001066// YAML conversion for const lld::SharedLibraryAtom*
1067template <>
1068struct MappingTraits<const lld::SharedLibraryAtom*> {
1069
1070 class NormalizedAtom : public lld::SharedLibraryAtom {
1071 public:
1072 NormalizedAtom(IO &io)
1073 : _file(fileFromContext(io)), _name(), _loadName(), _canBeNull(false) {
1074 }
1075 NormalizedAtom(IO &io, const lld::SharedLibraryAtom *atom)
Shankar Easwaran8962feb2013-03-14 16:09:49 +00001076 : _file(fileFromContext(io)),
1077 _name(atom->name()),
1078 _loadName(atom->loadName()),
Nick Kledzik6b079f52013-01-05 02:22:35 +00001079 _canBeNull(atom->canBeNullAtRuntime()) {
1080 }
1081 const lld::SharedLibraryAtom *denormalize(IO &io) {
1082 ContextInfo *info = reinterpret_cast<ContextInfo*>(io.getContext());
1083 assert(info != nullptr);
1084 typedef MappingTraits<const lld::File*>::NormalizedFile NormalizedFile;
1085 NormalizedFile *f = reinterpret_cast<NormalizedFile*>(info->_currentFile);
1086 if ( !_name.empty() )
1087 _name = f->copyString(_name);
1088 if ( !_loadName.empty() )
1089 _loadName = f->copyString(_loadName);
1090
Shankar Easwaran8962feb2013-03-14 16:09:49 +00001091 DEBUG_WITH_TYPE("WriterYAML", llvm::dbgs()
1092 << "created SharedLibraryAtom named: '" << _name
1093 << "' (" << (void*)_name.data() << ", "
Nick Kledzik6b079f52013-01-05 02:22:35 +00001094 << _name.size() << ")\n");
1095 return this;
1096 }
1097 // Extract current File object from YAML I/O parsing context
1098 const lld::File &fileFromContext(IO &io) {
1099 ContextInfo *info = reinterpret_cast<ContextInfo*>(io.getContext());
1100 assert(info != nullptr);
1101 assert(info->_currentFile != nullptr);
1102 return *info->_currentFile;
1103 }
1104
1105 virtual const lld::File &file() const { return _file; }
1106 virtual StringRef name() const { return _name; }
1107 virtual StringRef loadName() const { return _loadName;}
1108 virtual bool canBeNullAtRuntime() const { return _canBeNull; }
Shankar Easwaran8962feb2013-03-14 16:09:49 +00001109
Nick Kledzik6b079f52013-01-05 02:22:35 +00001110 const lld::File &_file;
1111 StringRef _name;
1112 StringRef _loadName;
1113 ShlibCanBeNull _canBeNull;
1114 };
1115
1116
1117 static void mapping(IO &io, const lld::SharedLibraryAtom *&atom) {
Shankar Easwaran8962feb2013-03-14 16:09:49 +00001118
1119 MappingNormalizationHeap<NormalizedAtom,
Nick Kledzik6b079f52013-01-05 02:22:35 +00001120 const lld::SharedLibraryAtom*> keys(io, atom);
1121
1122 io.mapRequired("name", keys->_name);
1123 io.mapOptional("load-name", keys->_loadName);
Shankar Easwaran8962feb2013-03-14 16:09:49 +00001124 io.mapOptional("can-be-null", keys->_canBeNull,
Nick Kledzik6b079f52013-01-05 02:22:35 +00001125 (ShlibCanBeNull)false);
1126 }
1127};
1128
1129
1130// YAML conversion for const lld::AbsoluteAtom*
1131template <>
1132struct MappingTraits<const lld::AbsoluteAtom*> {
1133
1134 class NormalizedAtom : public lld::AbsoluteAtom {
1135 public:
1136 NormalizedAtom(IO &io)
1137 : _file(fileFromContext(io)), _name(), _scope(), _value(0) {
1138 }
1139 NormalizedAtom(IO &io, const lld::AbsoluteAtom *atom)
Shankar Easwaran8962feb2013-03-14 16:09:49 +00001140 : _file(fileFromContext(io)),
1141 _name(atom->name()),
Nick Kledzik6b079f52013-01-05 02:22:35 +00001142 _scope(atom->scope()),
1143 _value(atom->value()) {
1144 }
1145 const lld::AbsoluteAtom *denormalize(IO &io) {
1146 ContextInfo *info = reinterpret_cast<ContextInfo*>(io.getContext());
1147 assert(info != nullptr);
1148 typedef MappingTraits<const lld::File*>::NormalizedFile NormalizedFile;
1149 NormalizedFile *f = reinterpret_cast<NormalizedFile*>(info->_currentFile);
1150 if ( !_name.empty() )
1151 _name = f->copyString(_name);
Shankar Easwaran8962feb2013-03-14 16:09:49 +00001152
1153 DEBUG_WITH_TYPE("WriterYAML", llvm::dbgs()
1154 << "created AbsoluteAtom named: '" << _name
1155 << "' (" << (void*)_name.data() << ", "
Nick Kledzik6b079f52013-01-05 02:22:35 +00001156 << _name.size() << ")\n");
1157 return this;
1158 }
1159 // Extract current File object from YAML I/O parsing context
1160 const lld::File &fileFromContext(IO &io) {
1161 ContextInfo *info = reinterpret_cast<ContextInfo*>(io.getContext());
1162 assert(info != nullptr);
1163 assert(info->_currentFile != nullptr);
1164 return *info->_currentFile;
1165 }
1166
1167 virtual const lld::File &file() const { return _file; }
1168 virtual StringRef name() const { return _name; }
1169 virtual uint64_t value() const { return _value; }
1170 virtual Scope scope() const { return _scope; }
Shankar Easwaran8962feb2013-03-14 16:09:49 +00001171
Nick Kledzik6b079f52013-01-05 02:22:35 +00001172 const lld::File &_file;
1173 StringRef _name;
1174 StringRef _refName;
1175 Scope _scope;
1176 Hex64 _value;
1177 };
1178
1179
1180 static void mapping(IO &io, const lld::AbsoluteAtom *&atom) {
Shankar Easwaran8962feb2013-03-14 16:09:49 +00001181 MappingNormalizationHeap<NormalizedAtom,
Nick Kledzik6b079f52013-01-05 02:22:35 +00001182 const lld::AbsoluteAtom*> keys(io, atom);
1183
1184 if ( io.outputting() ) {
1185 typedef MappingTraits<const lld::File*>::NormalizedFile NormalizedFile;
1186 ContextInfo *info = reinterpret_cast<ContextInfo*>(io.getContext());
1187 assert(info != nullptr);
1188 NormalizedFile *f = reinterpret_cast<NormalizedFile*>(info->_currentFile);
1189 assert(f);
1190 assert(f->_rnb);
1191 if ( f->_rnb->hasRefName(atom) ) {
1192 keys->_refName = f->_rnb->refName(atom);
1193 }
1194 }
1195
1196 io.mapRequired("name", keys->_name);
1197 io.mapOptional("ref-name", keys->_refName, StringRef());
1198 io.mapOptional("scope", keys->_scope);
1199 io.mapRequired("value", keys->_value);
1200 }
1201};
1202
Nick Kledzikbd491982013-01-08 23:51:03 +00001203} // namespace llvm
Shankar Easwaran8962feb2013-03-14 16:09:49 +00001204} // namespace yaml
Nick Kledzikbd491982013-01-08 23:51:03 +00001205
Nick Kledzik6b079f52013-01-05 02:22:35 +00001206
1207RefNameResolver::RefNameResolver(const lld::File *file, IO &io) : _io(io) {
1208 typedef MappingTraits<const lld::DefinedAtom*>::NormalizedAtom NormalizedAtom;
1209 for (const lld::DefinedAtom *a : file->defined() ) {
1210 NormalizedAtom *na = (NormalizedAtom*)a;
1211 if ( na->_refName.empty() )
1212 add(na->_name, a);
1213 else
1214 add(na->_refName, a);
1215 }
Shankar Easwaran8962feb2013-03-14 16:09:49 +00001216
Nick Kledzik6b079f52013-01-05 02:22:35 +00001217 for (const lld::UndefinedAtom *a : file->undefined() )
1218 add(a->name(), a);
Shankar Easwaran8962feb2013-03-14 16:09:49 +00001219
Nick Kledzik6b079f52013-01-05 02:22:35 +00001220 for (const lld::SharedLibraryAtom *a : file->sharedLibrary() )
1221 add(a->name(), a);
Shankar Easwaran8962feb2013-03-14 16:09:49 +00001222
Nick Kledzik6b079f52013-01-05 02:22:35 +00001223 typedef MappingTraits<const lld::AbsoluteAtom*>::NormalizedAtom NormAbsAtom;
1224 for (const lld::AbsoluteAtom *a : file->absolute() ) {
1225 NormAbsAtom *na = (NormAbsAtom*)a;
1226 if ( na->_refName.empty() )
1227 add(na->_name, a);
1228 else
1229 add(na->_refName, a);
1230 }
1231}
1232
1233
Nick Kledzikbd491982013-01-08 23:51:03 +00001234
Shankar Easwaran8962feb2013-03-14 16:09:49 +00001235inline
Nick Kledzikbd491982013-01-08 23:51:03 +00001236const lld::File*
1237MappingTraits<const lld::File*>::NormalizedFile::denormalize(IO &io) {
1238 typedef MappingTraits<const lld::DefinedAtom*>::NormalizedAtom NormalizedAtom;
Shankar Easwaran8962feb2013-03-14 16:09:49 +00001239
Nick Kledzikbd491982013-01-08 23:51:03 +00001240 RefNameResolver nameResolver(this, io);
1241 // Now that all atoms are parsed, references can be bound.
1242 for (const lld::DefinedAtom *a : this->defined() ) {
1243 NormalizedAtom *normAtom = (NormalizedAtom*)a;
1244 normAtom->bind(nameResolver);
1245 }
1246 return this;
1247}
1248
1249inline
1250void MappingTraits<const lld::DefinedAtom*>::
1251 NormalizedAtom::bind(const RefNameResolver &resolver) {
Shankar Easwaran8962feb2013-03-14 16:09:49 +00001252 typedef MappingTraits<const lld::Reference*>::NormalizedReference
Nick Kledzikbd491982013-01-08 23:51:03 +00001253 NormalizedReference;
1254 for (const lld::Reference *ref : _references) {
1255 NormalizedReference *normRef = (NormalizedReference*)ref;
1256 normRef->bind(resolver);
1257 }
1258}
1259
1260inline
1261void MappingTraits<const lld::Reference*>::
1262 NormalizedReference::bind(const RefNameResolver &resolver) {
1263 _target = resolver.lookup(_targetName);
1264}
1265
1266
Nick Kledzik6b079f52013-01-05 02:22:35 +00001267inline
1268llvm::StringRef MappingTraits<const lld::Reference*>::NormalizedReference::
1269 targetName(IO &io, const lld::Reference *ref) {
1270 if ( ref->target() == nullptr )
1271 return llvm::StringRef();
1272 ContextInfo *info = reinterpret_cast<ContextInfo*>(io.getContext());
1273 assert(info != nullptr);
1274 typedef MappingTraits<const lld::File*>::NormalizedFile NormalizedFile;
1275 NormalizedFile *f = reinterpret_cast<NormalizedFile*>(info->_currentFile);
1276 RefNameBuilder *rnb = f->_rnb;
Shankar Easwaran8962feb2013-03-14 16:09:49 +00001277 if ( rnb->hasRefName(ref->target()) )
Nick Kledzik6b079f52013-01-05 02:22:35 +00001278 return rnb->refName(ref->target());
1279 return ref->target()->name();
1280}
1281
1282
1283
1284namespace lld {
1285namespace yaml {
1286
1287class Writer : public lld::Writer {
1288public:
Michael J. Spencer64afcb42013-01-23 01:18:43 +00001289 Writer(const TargetInfo &ti) : _targetInfo(ti) {}
Shankar Easwaran8962feb2013-03-14 16:09:49 +00001290
Nick Kledzik6b079f52013-01-05 02:22:35 +00001291 virtual error_code writeFile(const lld::File &file, StringRef outPath) {
1292 // Create stream to path.
1293 std::string errorInfo;
1294 llvm::raw_fd_ostream out(outPath.data(), errorInfo);
1295 if (!errorInfo.empty())
1296 return llvm::make_error_code(llvm::errc::no_such_file_or_directory);
1297
1298 // Create yaml Output writer, using yaml options for context.
Michael J. Spencer64afcb42013-01-23 01:18:43 +00001299 ContextInfo context(_targetInfo);
Nick Kledzik6b079f52013-01-05 02:22:35 +00001300 llvm::yaml::Output yout(out, &context);
Shankar Easwaran8962feb2013-03-14 16:09:49 +00001301
Nick Kledzik6b079f52013-01-05 02:22:35 +00001302 // Write yaml output.
1303 const lld::File *fileRef = &file;
1304 yout << fileRef;
Shankar Easwaran8962feb2013-03-14 16:09:49 +00001305
Nick Kledzik6b079f52013-01-05 02:22:35 +00001306 return error_code::success();
1307 }
Shankar Easwaran8962feb2013-03-14 16:09:49 +00001308
Nick Kledzik6b079f52013-01-05 02:22:35 +00001309private:
Michael J. Spencer64afcb42013-01-23 01:18:43 +00001310 const TargetInfo &_targetInfo;
Nick Kledzik6b079f52013-01-05 02:22:35 +00001311};
1312
Nick Kledzik6b079f52013-01-05 02:22:35 +00001313class ReaderYAML : public Reader {
1314public:
Michael J. Spencer64afcb42013-01-23 01:18:43 +00001315 ReaderYAML(const TargetInfo &ti) : Reader(ti) {}
Nick Kledzik6b079f52013-01-05 02:22:35 +00001316
1317 error_code parseFile(std::unique_ptr<MemoryBuffer> mb,
1318 std::vector<std::unique_ptr<File>> &result) {
Shankar Easwaran8962feb2013-03-14 16:09:49 +00001319 // Note: we do not take ownership of the MemoryBuffer. That is
Nick Kledzik6b079f52013-01-05 02:22:35 +00001320 // because yaml may produce multiple File objects, so there is no
1321 // *one* File to take ownership. Therefore, the yaml File objects
1322 // produced must make copies of all strings that come from YAML I/O.
1323 // Otherwise the strings will become invalid when this MemoryBuffer
1324 // is deallocated.
1325
1326 // Create YAML Input parser.
Michael J. Spencer64afcb42013-01-23 01:18:43 +00001327 ContextInfo context(_targetInfo);
Nick Kledzik6b079f52013-01-05 02:22:35 +00001328 llvm::yaml::Input yin(mb->getBuffer(), &context);
Shankar Easwaran8962feb2013-03-14 16:09:49 +00001329
Nick Kledzik6b079f52013-01-05 02:22:35 +00001330 // Fill vector with File objects created by parsing yaml.
1331 std::vector<const lld::File*> createdFiles;
1332 yin >> createdFiles;
Shankar Easwaran8962feb2013-03-14 16:09:49 +00001333
Nick Kledzik6b079f52013-01-05 02:22:35 +00001334 // Quit now if there were parsing errors.
1335 if ( yin.error() )
1336 return make_error_code(lld::yaml_reader_error::illegal_value);
Shankar Easwaran8962feb2013-03-14 16:09:49 +00001337
Nick Kledzik6b079f52013-01-05 02:22:35 +00001338 for (const File *file : createdFiles) {
Shankar Easwaran8962feb2013-03-14 16:09:49 +00001339 // Note: parseFile() should return vector of *const* File
Nick Kledzik6b079f52013-01-05 02:22:35 +00001340 File *f = const_cast<File*>(file);
1341 result.emplace_back(f);
1342 }
1343 return make_error_code(lld::yaml_reader_error::success);
1344 }
Nick Kledzik6b079f52013-01-05 02:22:35 +00001345};
Michael J. Spencer64afcb42013-01-23 01:18:43 +00001346} // end namespace yaml
Nick Kledzik6b079f52013-01-05 02:22:35 +00001347
Michael J. Spencer64afcb42013-01-23 01:18:43 +00001348std::unique_ptr<Writer> createWriterYAML(const TargetInfo &ti) {
1349 return std::unique_ptr<Writer>(new lld::yaml::Writer(ti));
Nick Kledzik6b079f52013-01-05 02:22:35 +00001350}
1351
Michael J. Spencer64afcb42013-01-23 01:18:43 +00001352std::unique_ptr<Reader> createReaderYAML(const TargetInfo &ti) {
1353 return std::unique_ptr<Reader>(new lld::yaml::ReaderYAML(ti));
Nick Kledzik6b079f52013-01-05 02:22:35 +00001354}
Michael J. Spencer64afcb42013-01-23 01:18:43 +00001355} // end namespace lld