Nick Kledzik | 6b079f5 | 2013-01-05 02:22:35 +0000 | [diff] [blame] | 1 | //===- 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 | |
| 11 | #include "lld/Core/ArchiveLibraryFile.h" |
| 12 | #include "lld/Core/DefinedAtom.h" |
| 13 | #include "lld/Core/Error.h" |
| 14 | #include "lld/Core/File.h" |
| 15 | #include "lld/Core/LLVM.h" |
| 16 | #include "lld/Core/Reference.h" |
| 17 | #include "lld/ReaderWriter/ReaderYAML.h" |
| 18 | #include "lld/ReaderWriter/WriterYAML.h" |
| 19 | |
| 20 | #include "llvm/ADT/ArrayRef.h" |
| 21 | #include "llvm/ADT/OwningPtr.h" |
| 22 | #include "llvm/ADT/StringMap.h" |
| 23 | #include "llvm/ADT/Twine.h" |
| 24 | #include "llvm/Support/Debug.h" |
| 25 | #include "llvm/Support/ErrorHandling.h" |
| 26 | #include "llvm/Support/Format.h" |
| 27 | #include "llvm/Support/MemoryBuffer.h" |
| 28 | #include "llvm/Support/raw_ostream.h" |
| 29 | #include "llvm/Support/system_error.h" |
| 30 | #include "llvm/Support/YAMLTraits.h" |
| 31 | |
| 32 | #include <string> |
| 33 | |
| 34 | using llvm::yaml::MappingTraits; |
| 35 | using llvm::yaml::ScalarEnumerationTraits; |
| 36 | using llvm::yaml::ScalarTraits; |
| 37 | using llvm::yaml::IO; |
| 38 | using llvm::yaml::SequenceTraits; |
| 39 | using llvm::yaml::DocumentListTraits; |
| 40 | |
| 41 | |
| 42 | /// The conversion of Atoms to and from YAML uses LLVM's YAML I/O. This |
| 43 | /// file just defines template specializations on the lld types which control |
| 44 | /// how the mapping is done to and from YAML. |
| 45 | |
| 46 | |
| 47 | namespace { |
| 48 | /// Most of the traits are context-free and always do the same transformation. |
| 49 | /// But, there are some traits that need some contextual information to properly |
| 50 | /// do their transform. This struct is available via io.getContext() and |
| 51 | /// supplies contextual information. |
| 52 | class ContextInfo { |
| 53 | public: |
| 54 | ContextInfo(const lld::ReaderOptionsYAML &ro) |
| 55 | : _currentFile(nullptr), _readerOptions(&ro), _writerOptions(nullptr) { } |
| 56 | ContextInfo(const lld::WriterOptionsYAML &wo) |
| 57 | : _currentFile(nullptr), _readerOptions(nullptr), _writerOptions(&wo) { } |
| 58 | |
| 59 | lld::File *_currentFile; |
| 60 | const lld::ReaderOptionsYAML *_readerOptions; |
| 61 | const lld::WriterOptionsYAML *_writerOptions; |
| 62 | }; |
| 63 | |
| 64 | |
| 65 | /// Used when writing yaml files. |
| 66 | /// In most cases, atoms names are unambiguous, so references can just |
| 67 | /// use the atom name as the target (e.g. target: foo). But in a few |
| 68 | /// cases that does not work, so ref-names are added. These are labels |
| 69 | /// used only in yaml. The labels do not exist in the Atom model. |
| 70 | /// |
| 71 | /// One need for ref-names are when atoms have no user supplied name |
| 72 | /// (e.g. c-string literal). Another case is when two object files with |
| 73 | /// identically named static functions are merged (ld -r) into one object file. |
| 74 | /// In that case referencing the function by name is ambiguous, so a unique |
| 75 | /// ref-name is added. |
| 76 | class RefNameBuilder { |
| 77 | public: |
| 78 | RefNameBuilder(const lld::File &file) |
| 79 | : _collisionCount(0), _unnamedCounter(0) { |
| 80 | if (&file == nullptr) |
| 81 | return; |
| 82 | // visit all atoms |
| 83 | for (const lld::DefinedAtom *atom : file.defined()) { |
| 84 | // Build map of atoms names to detect duplicates |
| 85 | if (!atom->name().empty()) |
| 86 | buildDuplicateNameMap(*atom); |
| 87 | |
| 88 | // Find references to unnamed atoms and create ref-names for them. |
| 89 | for (const lld::Reference *ref : *atom) { |
| 90 | // create refname for any unnamed reference target |
| 91 | const lld::Atom *target = ref->target(); |
| 92 | if ((target != nullptr) && target->name().empty()) { |
| 93 | std::string storage; |
| 94 | llvm::raw_string_ostream buffer(storage); |
| 95 | buffer << llvm::format("L%03d", _unnamedCounter++); |
| 96 | llvm::StringRef newName = copyString(buffer.str()); |
| 97 | _refNames[target] = newName; |
| 98 | DEBUG_WITH_TYPE("WriterYAML", llvm::dbgs() |
| 99 | << "unnamed atom: creating ref-name: '" << newName |
| 100 | << "' (" << (void*)newName.data() << ", " |
| 101 | << newName.size() << ")\n"); |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | for (const lld::UndefinedAtom *undefAtom : file.undefined()) { |
| 106 | buildDuplicateNameMap(*undefAtom); |
| 107 | } |
| 108 | for (const lld::SharedLibraryAtom *shlibAtom : file.sharedLibrary()) { |
| 109 | buildDuplicateNameMap(*shlibAtom); |
| 110 | } |
| 111 | for (const lld::AbsoluteAtom *absAtom : file.absolute()) { |
| 112 | buildDuplicateNameMap(*absAtom); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | void buildDuplicateNameMap(const lld::Atom &atom) { |
| 117 | assert(!atom.name().empty()); |
| 118 | NameToAtom::iterator pos = _nameMap.find(atom.name()); |
| 119 | if ( pos != _nameMap.end() ) { |
| 120 | // Found name collision, give each a unique ref-name. |
| 121 | std::string Storage; |
| 122 | llvm::raw_string_ostream buffer(Storage); |
| 123 | buffer << atom.name() << llvm::format(".%03d", ++_collisionCount); |
| 124 | llvm::StringRef newName = copyString(buffer.str()); |
| 125 | _refNames[&atom] = newName; |
| 126 | DEBUG_WITH_TYPE("WriterYAML", llvm::dbgs() |
| 127 | << "name collsion: creating ref-name: '" << newName |
| 128 | << "' (" << (void*)newName.data() << ", " |
| 129 | << newName.size() << ")\n"); |
| 130 | const lld::Atom *prevAtom = pos->second; |
| 131 | AtomToRefName::iterator pos2 = _refNames.find(prevAtom); |
| 132 | if ( pos2 == _refNames.end() ) { |
| 133 | // Only create ref-name for previous if none already created. |
| 134 | std::string Storage2; |
| 135 | llvm::raw_string_ostream buffer2(Storage2); |
| 136 | buffer2 << prevAtom->name() << llvm::format(".%03d", ++_collisionCount); |
| 137 | llvm::StringRef newName2 = copyString(buffer2.str()); |
| 138 | _refNames[prevAtom] = newName2; |
| 139 | DEBUG_WITH_TYPE("WriterYAML", llvm::dbgs() |
| 140 | << "name collsion: creating ref-name: '" << newName2 |
| 141 | << "' (" << (void*)newName2.data() << ", " |
| 142 | << newName2.size() << ")\n"); |
| 143 | } |
| 144 | } |
| 145 | else { |
| 146 | // First time we've seen this name, just add it to map. |
| 147 | _nameMap[atom.name()] = &atom; |
| 148 | DEBUG_WITH_TYPE("WriterYAML", llvm::dbgs() |
| 149 | << "atom name seen for first time: '" << atom.name() |
| 150 | << "' (" << (void*)atom.name().data() << ", " |
| 151 | << atom.name().size() << ")\n"); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | bool hasRefName(const lld::Atom *atom) { |
| 156 | return _refNames.count(atom); |
| 157 | } |
| 158 | |
| 159 | llvm::StringRef refName(const lld::Atom *atom) { |
| 160 | return _refNames.find(atom)->second; |
| 161 | } |
| 162 | |
| 163 | private: |
| 164 | typedef llvm::StringMap<const lld::Atom*> NameToAtom; |
| 165 | typedef llvm::DenseMap<const lld::Atom*, std::string> AtomToRefName; |
| 166 | |
| 167 | // Allocate a new copy of this string and keep track of allocations |
| 168 | // in _stringCopies, so they can be freed when RefNameBuilder is destroyed. |
| 169 | llvm::StringRef copyString(llvm::StringRef str) { |
| 170 | // We want _stringCopies to own the string memory so it is deallocated |
| 171 | // when the File object is destroyed. But we need a StringRef that |
| 172 | // points into that memory. |
| 173 | std::unique_ptr<char> s = std::unique_ptr<char>(new char[str.size()]); |
| 174 | memcpy(s.get(), str.data(), str.size()); |
| 175 | llvm::StringRef r = llvm::StringRef(s.get(), str.size()); |
| 176 | _stringCopies.push_back(std::move(s)); |
| 177 | return r; |
| 178 | } |
| 179 | |
| 180 | unsigned int _collisionCount; |
| 181 | unsigned int _unnamedCounter; |
| 182 | NameToAtom _nameMap; |
| 183 | AtomToRefName _refNames; |
| 184 | std::vector<std::unique_ptr<char>> _stringCopies; |
| 185 | }; |
| 186 | |
| 187 | |
| 188 | /// Used when reading yaml files to find the target of a reference |
| 189 | /// that could be a name or ref-name. |
| 190 | class RefNameResolver { |
| 191 | public: |
| 192 | RefNameResolver(const lld::File *file, IO &io); |
| 193 | |
| 194 | const lld::Atom *lookup(llvm::StringRef name) const { |
| 195 | NameToAtom::const_iterator pos = _nameMap.find(name); |
| 196 | if (pos != _nameMap.end()) { |
| 197 | return pos->second; |
| 198 | } |
| 199 | else { |
| 200 | _io.setError(llvm::Twine("no such atom name: ") + name); |
| 201 | return nullptr; |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | private: |
| 206 | typedef llvm::StringMap<const lld::Atom*> NameToAtom; |
| 207 | |
| 208 | void add(llvm::StringRef name, const lld::Atom *atom) { |
| 209 | if (_nameMap.count(name)) { |
| 210 | _io.setError(llvm::Twine("duplicate atom name: ") + name); |
| 211 | } |
| 212 | else { |
| 213 | _nameMap[name] = atom; |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | IO &_io; |
| 218 | NameToAtom _nameMap; |
| 219 | }; |
| 220 | |
| 221 | |
| 222 | // Used in NormalizedFile to hold the atoms lists. |
| 223 | template <typename T> |
| 224 | class AtomList : public lld::File::atom_collection<T> { |
| 225 | public: |
| 226 | virtual lld::File::atom_iterator<T> begin() const { |
| 227 | return lld::File::atom_iterator<T>(*this, reinterpret_cast<const void*> |
| 228 | (_atoms.data())); |
| 229 | } |
| 230 | virtual lld::File::atom_iterator<T> end() const{ |
| 231 | return lld::File::atom_iterator<T>(*this, reinterpret_cast<const void*> |
| 232 | (_atoms.data() + _atoms.size())); |
| 233 | } |
| 234 | virtual const T *deref(const void *it) const { |
| 235 | return *reinterpret_cast<const T *const*>(it); |
| 236 | } |
| 237 | virtual void next(const void *&it) const { |
| 238 | const T *const *p = reinterpret_cast<const T *const *>(it); |
| 239 | ++p; |
| 240 | it = reinterpret_cast<const void*>(p); |
| 241 | } |
| 242 | virtual void push_back(const T *element) { |
| 243 | _atoms.push_back(element); |
| 244 | } |
| 245 | std::vector<const T*> _atoms; |
| 246 | }; |
| 247 | |
| 248 | /// Mapping of kind: field in yaml files. |
| 249 | enum FileKinds { |
| 250 | fileKindObjectAtoms, // atom based object file encoded in yaml |
| 251 | fileKindArchive, // static archive library encoded in yaml |
| 252 | fileKindObjectELF, // ELF object files encoded in yaml |
| 253 | fileKindObjectMachO // mach-o object files encoded in yaml |
| 254 | }; |
| 255 | |
| 256 | struct ArchMember { |
| 257 | FileKinds _kind; |
| 258 | llvm::StringRef _name; |
| 259 | const lld::File *_content; |
| 260 | }; |
| 261 | |
Nick Kledzik | bd49198 | 2013-01-08 23:51:03 +0000 | [diff] [blame] | 262 | |
Nick Kledzik | 6b079f5 | 2013-01-05 02:22:35 +0000 | [diff] [blame] | 263 | // The content bytes in a DefinedAtom are just uint8_t but we want |
| 264 | // special formatting, so define a strong type. |
Nick Kledzik | bd49198 | 2013-01-08 23:51:03 +0000 | [diff] [blame] | 265 | LLVM_YAML_STRONG_TYPEDEF(uint8_t, ImplicitHex8) |
Nick Kledzik | 6b079f5 | 2013-01-05 02:22:35 +0000 | [diff] [blame] | 266 | |
| 267 | // SharedLibraryAtoms have a bool canBeNull() method which we'd like to be |
| 268 | // more readable than just true/false. |
Nick Kledzik | bd49198 | 2013-01-08 23:51:03 +0000 | [diff] [blame] | 269 | LLVM_YAML_STRONG_TYPEDEF(bool, ShlibCanBeNull) |
Nick Kledzik | 6b079f5 | 2013-01-05 02:22:35 +0000 | [diff] [blame] | 270 | |
| 271 | // lld::Reference::Kind is a typedef of int32. We need a stronger |
| 272 | // type to make template matching work, so invent RefKind. |
Nick Kledzik | bd49198 | 2013-01-08 23:51:03 +0000 | [diff] [blame] | 273 | LLVM_YAML_STRONG_TYPEDEF(lld::Reference::Kind, RefKind) |
Nick Kledzik | 6b079f5 | 2013-01-05 02:22:35 +0000 | [diff] [blame] | 274 | |
| 275 | |
| 276 | } // namespace anon |
| 277 | |
| 278 | |
Nick Kledzik | bd49198 | 2013-01-08 23:51:03 +0000 | [diff] [blame] | 279 | LLVM_YAML_IS_SEQUENCE_VECTOR(ArchMember); |
| 280 | LLVM_YAML_IS_SEQUENCE_VECTOR(const lld::Reference*) |
| 281 | |
| 282 | // Always write DefinedAtoms content bytes as a flow sequence. |
| 283 | LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(ImplicitHex8); |
| 284 | |
| 285 | // for compatibility with gcc-4.7 in C++11 mode, add extra namespace |
| 286 | namespace llvm { |
| 287 | namespace yaml { |
| 288 | |
Nick Kledzik | 6b079f5 | 2013-01-05 02:22:35 +0000 | [diff] [blame] | 289 | // This is a custom formatter for RefKind |
| 290 | template<> |
| 291 | struct ScalarTraits<RefKind> { |
| 292 | static void output(const RefKind &value, void *ctxt, |
| 293 | llvm::raw_ostream &out) { |
| 294 | assert(ctxt != nullptr); |
| 295 | ContextInfo *info = reinterpret_cast<ContextInfo*>(ctxt); |
| 296 | out << info->_writerOptions->kindToString(value); |
| 297 | } |
| 298 | |
| 299 | static StringRef input(StringRef scalar, void *ctxt, RefKind &value) { |
| 300 | assert(ctxt != nullptr); |
| 301 | ContextInfo *info = reinterpret_cast<ContextInfo*>(ctxt); |
| 302 | value = info->_readerOptions->kindFromString(scalar); |
| 303 | return StringRef(); |
| 304 | } |
| 305 | }; |
| 306 | |
| 307 | |
| 308 | template <> |
| 309 | struct ScalarEnumerationTraits<lld::File::Kind> { |
| 310 | static void enumeration(IO &io, lld::File::Kind &value) { |
| 311 | io.enumCase(value, "object", lld::File::kindObject); |
| 312 | io.enumCase(value, "shared-library", lld::File::kindSharedLibrary); |
| 313 | io.enumCase(value, "static-library", lld::File::kindArchiveLibrary); |
| 314 | } |
| 315 | }; |
| 316 | |
| 317 | template <> |
| 318 | struct ScalarEnumerationTraits<lld::Atom::Scope> { |
| 319 | static void enumeration(IO &io, lld::Atom::Scope &value) { |
| 320 | io.enumCase(value, "global", lld::Atom::scopeGlobal); |
| 321 | io.enumCase(value, "hidden", lld::Atom::scopeLinkageUnit); |
| 322 | io.enumCase(value, "static", lld::Atom::scopeTranslationUnit); |
| 323 | } |
| 324 | }; |
| 325 | |
| 326 | template <> |
| 327 | struct ScalarEnumerationTraits<lld::DefinedAtom::SectionChoice> { |
| 328 | static void enumeration(IO &io, lld::DefinedAtom::SectionChoice &value) { |
| 329 | io.enumCase(value, "content", lld::DefinedAtom::sectionBasedOnContent); |
| 330 | io.enumCase(value, "custom", lld::DefinedAtom::sectionCustomPreferred); |
| 331 | io.enumCase(value, "custom-required", |
| 332 | lld::DefinedAtom::sectionCustomRequired); |
| 333 | } |
| 334 | }; |
| 335 | |
| 336 | template <> |
| 337 | struct ScalarEnumerationTraits<lld::DefinedAtom::Interposable> { |
| 338 | static void enumeration(IO &io, lld::DefinedAtom::Interposable &value) { |
| 339 | io.enumCase(value, "no", lld::DefinedAtom::interposeNo); |
| 340 | io.enumCase(value, "yes", lld::DefinedAtom::interposeYes); |
| 341 | io.enumCase(value, "yes-and-weak", |
| 342 | lld::DefinedAtom::interposeYesAndRuntimeWeak); |
| 343 | } |
| 344 | }; |
| 345 | |
| 346 | template <> |
| 347 | struct ScalarEnumerationTraits<lld::DefinedAtom::Merge> { |
| 348 | static void enumeration(IO &io, lld::DefinedAtom::Merge &value) { |
| 349 | io.enumCase(value, "no", lld::DefinedAtom::mergeNo); |
| 350 | io.enumCase(value, "as-tentative", lld::DefinedAtom::mergeAsTentative); |
| 351 | io.enumCase(value, "as-weak", lld::DefinedAtom::mergeAsWeak); |
| 352 | io.enumCase(value, "as-addressed-weak", |
| 353 | lld::DefinedAtom::mergeAsWeakAndAddressUsed); |
| 354 | } |
| 355 | }; |
| 356 | |
| 357 | template <> |
| 358 | struct ScalarEnumerationTraits<lld::DefinedAtom::DeadStripKind> { |
| 359 | static void enumeration(IO &io, lld::DefinedAtom::DeadStripKind &value) { |
| 360 | io.enumCase(value, "normal", lld::DefinedAtom::deadStripNormal); |
| 361 | io.enumCase(value, "never", lld::DefinedAtom::deadStripNever); |
| 362 | io.enumCase(value, "always", lld::DefinedAtom::deadStripAlways); |
| 363 | } |
| 364 | }; |
| 365 | |
| 366 | template <> |
| 367 | struct ScalarEnumerationTraits<lld::DefinedAtom::ContentPermissions> { |
| 368 | static void enumeration(IO &io, lld::DefinedAtom::ContentPermissions &value) { |
Nick Kledzik | cc3d2dc | 2013-01-09 01:17:12 +0000 | [diff] [blame] | 369 | io.enumCase(value, "---", lld::DefinedAtom::perm___); |
| 370 | io.enumCase(value, "r--", lld::DefinedAtom::permR__); |
| 371 | io.enumCase(value, "r-x", lld::DefinedAtom::permR_X); |
| 372 | io.enumCase(value, "rw-", lld::DefinedAtom::permRW_); |
| 373 | io.enumCase(value, "rwx", lld::DefinedAtom::permRWX); |
| 374 | io.enumCase(value, "rw-l", lld::DefinedAtom::permRW_L); |
| 375 | io.enumCase(value, "unknown", lld::DefinedAtom::permUnknown); |
Nick Kledzik | 6b079f5 | 2013-01-05 02:22:35 +0000 | [diff] [blame] | 376 | } |
| 377 | }; |
| 378 | |
| 379 | template <> |
| 380 | struct ScalarEnumerationTraits<lld::DefinedAtom::ContentType> { |
| 381 | static void enumeration(IO &io, lld::DefinedAtom::ContentType &value) { |
| 382 | io.enumCase(value, "unknown", |
| 383 | lld::DefinedAtom::typeUnknown); |
| 384 | io.enumCase(value, "code", |
| 385 | lld::DefinedAtom::typeCode); |
| 386 | io.enumCase(value, "stub", |
| 387 | lld::DefinedAtom::typeStub); |
| 388 | io.enumCase(value, "constant", |
| 389 | lld::DefinedAtom::typeConstant); |
| 390 | io.enumCase(value, "data", |
| 391 | lld::DefinedAtom::typeData); |
| 392 | io.enumCase(value, "zero-fill", |
| 393 | lld::DefinedAtom::typeZeroFill); |
Nick Kledzik | cc3d2dc | 2013-01-09 01:17:12 +0000 | [diff] [blame] | 394 | io.enumCase(value, "const-data", |
| 395 | lld::DefinedAtom::typeConstData); |
Nick Kledzik | 6b079f5 | 2013-01-05 02:22:35 +0000 | [diff] [blame] | 396 | io.enumCase(value, "got", |
| 397 | lld::DefinedAtom::typeGOT); |
| 398 | io.enumCase(value, "resolver", |
| 399 | lld::DefinedAtom::typeResolver); |
| 400 | io.enumCase(value, "branch-island", |
| 401 | lld::DefinedAtom::typeBranchIsland); |
| 402 | io.enumCase(value, "branch-shim", |
| 403 | lld::DefinedAtom::typeBranchShim); |
| 404 | io.enumCase(value, "stub-helper", |
| 405 | lld::DefinedAtom::typeStubHelper); |
| 406 | io.enumCase(value, "c-string", |
| 407 | lld::DefinedAtom::typeCString); |
| 408 | io.enumCase(value, "utf16-string", |
| 409 | lld::DefinedAtom::typeUTF16String); |
| 410 | io.enumCase(value, "unwind-cfi", |
| 411 | lld::DefinedAtom::typeCFI); |
| 412 | io.enumCase(value, "unwind-lsda", |
| 413 | lld::DefinedAtom::typeLSDA); |
| 414 | io.enumCase(value, "const-4-byte", |
| 415 | lld::DefinedAtom::typeLiteral4); |
| 416 | io.enumCase(value, "const-8-byte", |
| 417 | lld::DefinedAtom::typeLiteral8); |
| 418 | io.enumCase(value, "const-16-byte", |
| 419 | lld::DefinedAtom::typeLiteral16); |
| 420 | io.enumCase(value, "lazy-pointer", |
| 421 | lld::DefinedAtom::typeLazyPointer); |
| 422 | io.enumCase(value, "lazy-dylib-pointer", |
| 423 | lld::DefinedAtom::typeLazyDylibPointer); |
| 424 | io.enumCase(value, "cfstring", |
| 425 | lld::DefinedAtom::typeCFString); |
| 426 | io.enumCase(value, "initializer-pointer", |
| 427 | lld::DefinedAtom::typeInitializerPtr); |
| 428 | io.enumCase(value, "terminator-pointer", |
| 429 | lld::DefinedAtom::typeTerminatorPtr); |
| 430 | io.enumCase(value, "c-string-pointer", |
| 431 | lld::DefinedAtom::typeCStringPtr); |
| 432 | io.enumCase(value, "objc-class-pointer", |
| 433 | lld::DefinedAtom::typeObjCClassPtr); |
| 434 | io.enumCase(value, "objc-category-list", |
| 435 | lld::DefinedAtom::typeObjC2CategoryList); |
| 436 | io.enumCase(value, "objc-class1", |
| 437 | lld::DefinedAtom::typeObjC1Class); |
| 438 | io.enumCase(value, "dtraceDOF", |
| 439 | lld::DefinedAtom::typeDTraceDOF); |
| 440 | io.enumCase(value, "lto-temp", |
| 441 | lld::DefinedAtom::typeTempLTO); |
| 442 | io.enumCase(value, "compact-unwind", |
| 443 | lld::DefinedAtom::typeCompactUnwindInfo); |
| 444 | io.enumCase(value, "tlv-thunk", |
| 445 | lld::DefinedAtom::typeThunkTLV); |
| 446 | io.enumCase(value, "tlv-data", |
| 447 | lld::DefinedAtom::typeTLVInitialData); |
| 448 | io.enumCase(value, "tlv-zero-fill", |
| 449 | lld::DefinedAtom::typeTLVInitialZeroFill); |
| 450 | io.enumCase(value, "tlv-initializer-ptr", |
| 451 | lld::DefinedAtom::typeTLVInitializerPtr); |
| 452 | io.enumCase(value, "first-in-section", |
| 453 | lld::DefinedAtom::typeFirstInSection); |
| 454 | io.enumCase(value, "last-in-section", |
| 455 | lld::DefinedAtom::typeLastInSection); |
| 456 | } |
| 457 | }; |
| 458 | |
| 459 | template <> |
| 460 | struct ScalarEnumerationTraits<lld::UndefinedAtom::CanBeNull> { |
| 461 | static void enumeration(IO &io, lld::UndefinedAtom::CanBeNull &value) { |
| 462 | io.enumCase(value, "never", lld::UndefinedAtom::canBeNullNever); |
| 463 | io.enumCase(value, "at-runtime", lld::UndefinedAtom::canBeNullAtRuntime); |
| 464 | io.enumCase(value, "at-buildtime", lld::UndefinedAtom::canBeNullAtBuildtime); |
| 465 | } |
| 466 | }; |
| 467 | |
| 468 | |
| 469 | template <> |
| 470 | struct ScalarEnumerationTraits<ShlibCanBeNull> { |
| 471 | static void enumeration(IO &io, ShlibCanBeNull &value) { |
| 472 | io.enumCase(value, "never", false); |
| 473 | io.enumCase(value, "at-runtime", true); |
| 474 | } |
| 475 | }; |
| 476 | |
| 477 | |
| 478 | |
| 479 | /// This is a custom formatter for lld::DefinedAtom::Alignment. Values look |
| 480 | /// like: |
| 481 | /// 2^3 # 8-byte aligned |
| 482 | /// 7 mod 2^4 # 16-byte aligned plus 7 bytes |
| 483 | template<> |
| 484 | struct ScalarTraits<lld::DefinedAtom::Alignment> { |
| 485 | static void output(const lld::DefinedAtom::Alignment &value, void *ctxt, |
| 486 | llvm::raw_ostream &out) { |
| 487 | if (value.modulus == 0) { |
| 488 | out << llvm::format("2^%d", value.powerOf2); |
| 489 | } |
| 490 | else { |
| 491 | out << llvm::format("%d mod 2^%d", value.modulus, value.powerOf2); |
| 492 | } |
| 493 | } |
| 494 | |
| 495 | static StringRef input(StringRef scalar, void *ctxt, |
| 496 | lld::DefinedAtom::Alignment &value) { |
| 497 | value.modulus = 0; |
| 498 | size_t modStart = scalar.find("mod"); |
| 499 | if (modStart != StringRef::npos) { |
| 500 | StringRef modStr = scalar.slice(0, modStart); |
| 501 | modStr = modStr.rtrim(); |
| 502 | unsigned int modulus; |
| 503 | if (modStr.getAsInteger(0, modulus)) { |
| 504 | return "malformed alignment modulus"; |
| 505 | } |
| 506 | value.modulus = modulus; |
| 507 | scalar = scalar.drop_front(modStart+3); |
| 508 | scalar = scalar.ltrim(); |
| 509 | } |
| 510 | if (!scalar.startswith("2^")) { |
| 511 | return "malformed alignment"; |
| 512 | } |
| 513 | StringRef powerStr = scalar.drop_front(2); |
| 514 | unsigned int power; |
| 515 | if (powerStr.getAsInteger(0, power)) { |
| 516 | return "malformed alignment power"; |
| 517 | } |
| 518 | value.powerOf2 = power; |
| 519 | if (value.modulus > (1<<value.powerOf2)) { |
| 520 | return "malformed alignment, modulus too large for power"; |
| 521 | } |
| 522 | return StringRef(); // returning empty string means success |
| 523 | } |
| 524 | }; |
| 525 | |
| 526 | |
| 527 | |
| 528 | |
| 529 | template <> |
| 530 | struct ScalarEnumerationTraits<FileKinds> { |
| 531 | static void enumeration(IO &io, FileKinds &value) { |
| 532 | io.enumCase(value, "object", fileKindObjectAtoms); |
| 533 | io.enumCase(value, "archive", fileKindArchive); |
| 534 | io.enumCase(value, "object-elf", fileKindObjectELF); |
| 535 | io.enumCase(value, "object-mach-o", fileKindObjectMachO); |
| 536 | } |
| 537 | }; |
| 538 | |
| 539 | template <> |
| 540 | struct MappingTraits<ArchMember> { |
| 541 | static void mapping(IO &io, ArchMember &member) { |
| 542 | io.mapOptional("kind", member._kind, fileKindObjectAtoms); |
| 543 | io.mapOptional("name", member._name); |
| 544 | io.mapRequired("content", member._content); |
| 545 | } |
| 546 | }; |
| 547 | |
Nick Kledzik | 6b079f5 | 2013-01-05 02:22:35 +0000 | [diff] [blame] | 548 | |
| 549 | |
| 550 | // Declare that an AtomList is a yaml sequence. |
| 551 | template<typename T> |
| 552 | struct SequenceTraits<AtomList<T>> { |
| 553 | static size_t size(IO &io, AtomList<T> &seq) { |
| 554 | return seq._atoms.size(); |
| 555 | } |
| 556 | static const T *&element(IO &io, AtomList<T> &seq, size_t index) { |
| 557 | if (index >= seq._atoms.size()) |
| 558 | seq._atoms.resize(index+1); |
| 559 | return seq._atoms[index]; |
| 560 | } |
| 561 | }; |
| 562 | |
| 563 | // Used to allow DefinedAtom content bytes to be a flow sequence of |
| 564 | // two-digit hex numbers without the leading 0x (e.g. FF, 04, 0A) |
| 565 | template<> |
| 566 | struct ScalarTraits<ImplicitHex8> { |
| 567 | static void output(const ImplicitHex8 &val, void*, llvm::raw_ostream &out) { |
| 568 | uint8_t num = val; |
| 569 | out << llvm::format("%02X", num); |
| 570 | } |
| 571 | |
| 572 | static llvm::StringRef input(llvm::StringRef str, void*, ImplicitHex8 &val) { |
| 573 | unsigned long long n; |
| 574 | if (getAsUnsignedInteger(str, 16, n)) |
| 575 | return "invalid two-digit-hex number"; |
| 576 | if (n > 0xFF) |
| 577 | return "out of range two-digit-hex number"; |
| 578 | val = n; |
| 579 | return StringRef(); // returning empty string means success |
| 580 | } |
| 581 | }; |
| 582 | |
Nick Kledzik | 6b079f5 | 2013-01-05 02:22:35 +0000 | [diff] [blame] | 583 | |
| 584 | // YAML conversion for std::vector<const lld::File*> |
| 585 | template<> |
| 586 | struct DocumentListTraits< std::vector<const lld::File*> > { |
| 587 | static size_t size(IO &io, std::vector<const lld::File*> &seq) { |
| 588 | return seq.size(); |
| 589 | } |
| 590 | static const lld::File *&element(IO &io, std::vector<const lld::File*> &seq, |
| 591 | size_t index) { |
| 592 | if (index >= seq.size()) |
| 593 | seq.resize(index+1); |
| 594 | return seq[index]; |
| 595 | } |
| 596 | }; |
| 597 | |
| 598 | |
| 599 | // YAML conversion for const lld::File* |
| 600 | template <> |
| 601 | struct MappingTraits<const lld::File*> { |
| 602 | |
| 603 | class NormArchiveFile : public lld::ArchiveLibraryFile { |
| 604 | public: |
| 605 | NormArchiveFile(IO &io) : ArchiveLibraryFile(""), _path() { |
| 606 | } |
| 607 | NormArchiveFile(IO &io, const lld::File *file) |
| 608 | : ArchiveLibraryFile(file->path()), |
| 609 | _path(file->path()) { |
| 610 | // If we want to support writing archives, this constructor would |
| 611 | // need to populate _members. |
| 612 | } |
| 613 | |
| 614 | const lld::File *denormalize(IO &io) { |
| 615 | return this; |
| 616 | } |
| 617 | |
| 618 | virtual void addAtom(const lld::Atom&) { |
| 619 | llvm_unreachable("cannot add atoms to yaml .o files"); |
| 620 | } |
| 621 | virtual const atom_collection<lld::DefinedAtom> &defined() const { |
| 622 | return _noDefinedAtoms; |
| 623 | } |
| 624 | virtual const atom_collection<lld::UndefinedAtom> &undefined() const { |
| 625 | return _noUndefinedAtoms; |
| 626 | } |
| 627 | virtual const atom_collection<lld::SharedLibraryAtom> &sharedLibrary()const{ |
| 628 | return _noSharedLibaryAtoms; |
| 629 | } |
| 630 | virtual const atom_collection<lld::AbsoluteAtom> &absolute() const { |
| 631 | return _noAbsoluteAtoms; |
| 632 | } |
| 633 | virtual const File *find(StringRef name, bool dataSymbolOnly) const { |
| 634 | for (const ArchMember &member : _members) { |
| 635 | for (const lld::DefinedAtom *atom : member._content->defined() ) { |
| 636 | if (name == atom->name()) { |
| 637 | if (!dataSymbolOnly) |
| 638 | return member._content; |
| 639 | switch (atom->contentType()) { |
| 640 | case lld::DefinedAtom::typeData: |
| 641 | case lld::DefinedAtom::typeZeroFill: |
| 642 | return member._content; |
| 643 | default: |
| 644 | break; |
| 645 | } |
| 646 | } |
| 647 | } |
| 648 | } |
| 649 | return nullptr; |
| 650 | } |
| 651 | |
| 652 | StringRef _path; |
| 653 | std::vector<ArchMember> _members; |
| 654 | }; |
| 655 | |
| 656 | |
| 657 | class NormalizedFile : public lld::File { |
| 658 | public: |
| 659 | NormalizedFile(IO &io) : File(""), _rnb(nullptr) { |
| 660 | } |
| 661 | NormalizedFile(IO &io, const lld::File *file) |
| 662 | : File(file->path()), |
| 663 | _rnb(new RefNameBuilder(*file)), |
| 664 | _path(file->path()) { |
| 665 | for (const lld::DefinedAtom *a : file->defined()) |
| 666 | _definedAtoms.push_back(a); |
| 667 | for (const lld::UndefinedAtom *a : file->undefined()) |
| 668 | _undefinedAtoms.push_back(a); |
| 669 | for (const lld::SharedLibraryAtom *a : file->sharedLibrary()) |
| 670 | _sharedLibraryAtoms.push_back(a); |
| 671 | for (const lld::AbsoluteAtom *a : file->absolute()) |
| 672 | _absoluteAtoms.push_back(a); |
| 673 | } |
| 674 | const lld::File *denormalize(IO &io); |
| 675 | |
| 676 | |
| 677 | virtual void addAtom(const lld::Atom&) { |
| 678 | llvm_unreachable("cannot add atoms to yaml .o files"); |
| 679 | } |
| 680 | virtual const atom_collection<lld::DefinedAtom> &defined() const { |
| 681 | return _definedAtoms; |
| 682 | } |
| 683 | virtual const atom_collection<lld::UndefinedAtom> &undefined() const { |
| 684 | return _undefinedAtoms; |
| 685 | } |
| 686 | virtual const atom_collection<lld::SharedLibraryAtom> &sharedLibrary()const{ |
| 687 | return _sharedLibraryAtoms; |
| 688 | } |
| 689 | virtual const atom_collection<lld::AbsoluteAtom> &absolute() const { |
| 690 | return _absoluteAtoms; |
| 691 | } |
| 692 | |
| 693 | // Allocate a new copy of this string and keep track of allocations |
| 694 | // in _stringCopies, so they can be freed when File is destroyed. |
| 695 | StringRef copyString(StringRef str) { |
| 696 | // We want _stringCopies to own the string memory so it is deallocated |
| 697 | // when the File object is destroyed. But we need a StringRef that |
| 698 | // points into that memory. |
| 699 | std::unique_ptr<char> s = std::unique_ptr<char>(new char[str.size()]); |
| 700 | memcpy(s.get(), str.data(), str.size()); |
| 701 | llvm::StringRef r = llvm::StringRef(s.get(), str.size()); |
| 702 | _stringCopies.push_back(std::move(s)); |
| 703 | return r; |
| 704 | } |
| 705 | |
| 706 | RefNameBuilder *_rnb; |
| 707 | StringRef _path; |
| 708 | AtomList<lld::DefinedAtom> _definedAtoms; |
| 709 | AtomList<lld::UndefinedAtom> _undefinedAtoms; |
| 710 | AtomList<lld::SharedLibraryAtom> _sharedLibraryAtoms; |
| 711 | AtomList<lld::AbsoluteAtom> _absoluteAtoms; |
| 712 | std::vector<std::unique_ptr<char>> _stringCopies; |
| 713 | }; |
| 714 | |
| 715 | |
| 716 | static void mapping(IO &io, const lld::File *&file) { |
| 717 | // We only support writing atom based YAML |
| 718 | FileKinds kind = fileKindObjectAtoms; |
| 719 | // If reading, peek ahead to see what kind of file this is. |
| 720 | io.mapOptional("kind", kind, fileKindObjectAtoms); |
| 721 | // |
| 722 | switch (kind) { |
| 723 | case fileKindObjectAtoms: |
| 724 | mappingAtoms(io, file); |
| 725 | break; |
| 726 | case fileKindArchive: |
| 727 | mappingArchive(io, file); |
| 728 | break; |
| 729 | case fileKindObjectELF: |
| 730 | case fileKindObjectMachO: |
| 731 | // Eventually we will have an external function to call, similar |
| 732 | // to mappingAtoms() and mappingArchive() but implememented |
| 733 | // with coresponding file format code. |
Nick Kledzik | 6b079f5 | 2013-01-05 02:22:35 +0000 | [diff] [blame] | 734 | llvm_unreachable("section based YAML not supported yet"); |
| 735 | } |
| 736 | } |
| 737 | |
| 738 | static void mappingAtoms(IO &io, const lld::File *&file) { |
| 739 | MappingNormalizationHeap<NormalizedFile, const lld::File*> keys(io, file); |
| 740 | ContextInfo *info = reinterpret_cast<ContextInfo*>(io.getContext()); |
| 741 | assert(info != nullptr); |
| 742 | info->_currentFile = keys.operator->(); |
| 743 | |
| 744 | io.mapOptional("path", keys->_path); |
| 745 | io.mapOptional("defined-atoms", keys->_definedAtoms); |
| 746 | io.mapOptional("undefined-atoms", keys->_undefinedAtoms); |
| 747 | io.mapOptional("shared-library-atoms", keys->_sharedLibraryAtoms); |
| 748 | io.mapOptional("absolute-atoms", keys->_absoluteAtoms); |
| 749 | } |
| 750 | |
| 751 | static void mappingArchive(IO &io, const lld::File *&file) { |
| 752 | MappingNormalizationHeap<NormArchiveFile, const lld::File*> keys(io, file); |
| 753 | |
| 754 | io.mapOptional("path", keys->_path); |
| 755 | io.mapOptional("members", keys->_members); |
| 756 | } |
| 757 | |
| 758 | }; |
| 759 | |
| 760 | |
| 761 | |
| 762 | // YAML conversion for const lld::Reference* |
| 763 | template <> |
| 764 | struct MappingTraits<const lld::Reference*> { |
| 765 | |
| 766 | class NormalizedReference : public lld::Reference { |
| 767 | public: |
| 768 | NormalizedReference(IO &io) |
| 769 | : _target(nullptr), _targetName(), _offset(0), _addend(0) , _kind(0) { |
| 770 | } |
| 771 | NormalizedReference(IO &io, const lld::Reference *ref) |
| 772 | : _target(nullptr), |
| 773 | _targetName(targetName(io, ref)), |
| 774 | _offset(ref->offsetInAtom()), |
| 775 | _addend(ref->addend()), |
| 776 | _kind(ref->kind()) { |
| 777 | } |
| 778 | const lld::Reference *denormalize(IO &io) { |
| 779 | ContextInfo *info = reinterpret_cast<ContextInfo*>(io.getContext()); |
| 780 | assert(info != nullptr); |
| 781 | typedef MappingTraits<const lld::File*>::NormalizedFile NormalizedFile; |
| 782 | NormalizedFile *f = reinterpret_cast<NormalizedFile*>(info->_currentFile); |
| 783 | if (!_targetName.empty()) |
| 784 | _targetName = f->copyString(_targetName); |
| 785 | DEBUG_WITH_TYPE("WriterYAML", llvm::dbgs() |
| 786 | << "created Reference to name: '" << _targetName |
| 787 | << "' (" << (void*)_targetName.data() << ", " |
| 788 | << _targetName.size() << ")\n"); |
| 789 | return this; |
| 790 | } |
| 791 | void bind(const RefNameResolver&); |
| 792 | static StringRef targetName(IO &io, const lld::Reference *ref); |
| 793 | |
| 794 | virtual uint64_t offsetInAtom() const { return _offset; } |
| 795 | virtual Kind kind() const { return _kind; } |
| 796 | virtual const lld::Atom *target() const { return _target; } |
| 797 | virtual Addend addend() const { return _addend; } |
| 798 | virtual void setKind(Kind k) { _kind = k; } |
| 799 | virtual void setAddend(Addend a) { _addend = a; } |
| 800 | virtual void setTarget(const lld::Atom *a) { _target = a; } |
| 801 | |
| 802 | const lld::Atom *_target; |
| 803 | StringRef _targetName; |
| 804 | uint32_t _offset; |
| 805 | Addend _addend; |
| 806 | RefKind _kind; |
| 807 | }; |
| 808 | |
| 809 | |
| 810 | static void mapping(IO &io, const lld::Reference *&ref) { |
| 811 | MappingNormalizationHeap<NormalizedReference, |
| 812 | const lld::Reference*> keys(io, ref); |
| 813 | |
| 814 | io.mapRequired("kind", keys->_kind); |
| 815 | io.mapOptional("offset", keys->_offset); |
| 816 | io.mapOptional("target", keys->_targetName); |
| 817 | io.mapOptional("addend", keys->_addend, (lld::Reference::Addend)0); |
| 818 | } |
| 819 | }; |
| 820 | |
Nick Kledzik | 6b079f5 | 2013-01-05 02:22:35 +0000 | [diff] [blame] | 821 | |
| 822 | |
| 823 | // YAML conversion for const lld::DefinedAtom* |
| 824 | template <> |
| 825 | struct MappingTraits<const lld::DefinedAtom*> { |
| 826 | |
| 827 | class NormalizedAtom : public lld::DefinedAtom { |
| 828 | public: |
| 829 | NormalizedAtom(IO &io) |
| 830 | : _file(fileFromContext(io)), _name(), _refName(), |
| 831 | _alignment(0), _content(), _references() { |
| 832 | } |
| 833 | NormalizedAtom(IO &io, const lld::DefinedAtom *atom) |
| 834 | : _file(fileFromContext(io)), |
| 835 | _name(atom->name()), |
| 836 | _refName(), |
| 837 | _scope(atom->scope()), |
| 838 | _interpose(atom->interposable()), |
| 839 | _merge(atom->merge()), |
| 840 | _contentType(atom->contentType()), |
| 841 | _alignment(atom->alignment()), |
| 842 | _sectionChoice(atom->sectionChoice()), |
| 843 | _deadStrip(atom->deadStrip()), |
| 844 | _permissions(atom->permissions()), |
| 845 | _size(atom->size()), |
| 846 | _sectionName(atom->customSectionName()) { |
| 847 | for ( const lld::Reference *r : *atom ) |
| 848 | _references.push_back(r); |
| 849 | ArrayRef<uint8_t> cont = atom->rawContent(); |
| 850 | _content.reserve(cont.size()); |
| 851 | for (uint8_t x : cont) |
| 852 | _content.push_back(x); |
| 853 | } |
| 854 | const lld::DefinedAtom *denormalize(IO &io) { |
| 855 | ContextInfo *info = reinterpret_cast<ContextInfo*>(io.getContext()); |
| 856 | assert(info != nullptr); |
| 857 | typedef MappingTraits<const lld::File*>::NormalizedFile NormalizedFile; |
| 858 | NormalizedFile *f = reinterpret_cast<NormalizedFile*>(info->_currentFile); |
| 859 | if ( !_name.empty() ) |
| 860 | _name = f->copyString(_name); |
| 861 | if ( !_refName.empty() ) |
| 862 | _refName = f->copyString(_refName); |
| 863 | if ( !_sectionName.empty() ) |
| 864 | _sectionName = f->copyString(_sectionName); |
| 865 | DEBUG_WITH_TYPE("WriterYAML", llvm::dbgs() |
| 866 | << "created DefinedAtom named: '" << _name |
| 867 | << "' (" << (void*)_name.data() << ", " |
| 868 | << _name.size() << ")\n"); |
| 869 | return this; |
| 870 | } |
| 871 | void bind(const RefNameResolver&); |
| 872 | // Extract current File object from YAML I/O parsing context |
| 873 | const lld::File &fileFromContext(IO &io) { |
| 874 | ContextInfo *info = reinterpret_cast<ContextInfo*>(io.getContext()); |
| 875 | assert(info != nullptr); |
| 876 | assert(info->_currentFile != nullptr); |
| 877 | return *info->_currentFile; |
| 878 | } |
| 879 | |
| 880 | virtual const lld::File &file() const { return _file; } |
| 881 | virtual StringRef name() const { return _name; } |
| 882 | virtual uint64_t size() const { return _size; } |
| 883 | virtual Scope scope() const { return _scope; } |
| 884 | virtual Interposable interposable() const { return _interpose; } |
| 885 | virtual Merge merge() const { return _merge; } |
| 886 | virtual ContentType contentType() const { return _contentType; } |
| 887 | virtual Alignment alignment() const { return _alignment; } |
| 888 | virtual SectionChoice sectionChoice() const { return _sectionChoice; } |
| 889 | virtual StringRef customSectionName() const { return _sectionName;} |
| 890 | virtual DeadStripKind deadStrip() const { return _deadStrip; } |
| 891 | virtual ContentPermissions permissions() const { return _permissions; } |
| 892 | virtual bool isThumb() const { return false; } |
| 893 | virtual bool isAlias() const { return false; } |
| 894 | ArrayRef<uint8_t> rawContent() const { |
| 895 | return ArrayRef<uint8_t>((uint8_t*)&_content.operator[](0), |
| 896 | _content.size()); } |
| 897 | virtual uint64_t ordinal() const { return 0; } |
| 898 | |
| 899 | reference_iterator begin() const { |
| 900 | uintptr_t index = 0; |
| 901 | const void *it = reinterpret_cast<const void*>(index); |
| 902 | return reference_iterator(*this, it); |
| 903 | } |
| 904 | reference_iterator end() const { |
| 905 | uintptr_t index = _references.size(); |
| 906 | const void *it = reinterpret_cast<const void*>(index); |
| 907 | return reference_iterator(*this, it); |
| 908 | } |
| 909 | const lld::Reference *derefIterator(const void *it) const { |
| 910 | uintptr_t index = reinterpret_cast<uintptr_t>(it); |
| 911 | assert(index < _references.size()); |
| 912 | return _references[index]; |
| 913 | } |
| 914 | void incrementIterator(const void *&it) const { |
| 915 | uintptr_t index = reinterpret_cast<uintptr_t>(it); |
| 916 | ++index; |
| 917 | it = reinterpret_cast<const void*>(index); |
| 918 | } |
| 919 | |
| 920 | const lld::File &_file; |
| 921 | StringRef _name; |
| 922 | StringRef _refName; |
| 923 | Scope _scope; |
| 924 | Interposable _interpose; |
| 925 | Merge _merge; |
| 926 | ContentType _contentType; |
| 927 | Alignment _alignment; |
| 928 | SectionChoice _sectionChoice; |
| 929 | DeadStripKind _deadStrip; |
| 930 | ContentPermissions _permissions; |
| 931 | std::vector<ImplicitHex8> _content; |
| 932 | uint64_t _size; |
| 933 | StringRef _sectionName; |
| 934 | std::vector<const lld::Reference*> _references; |
| 935 | }; |
| 936 | |
| 937 | static void mapping(IO &io, const lld::DefinedAtom *&atom) { |
| 938 | MappingNormalizationHeap<NormalizedAtom, |
| 939 | const lld::DefinedAtom*> keys(io, atom); |
| 940 | if ( io.outputting() ) { |
| 941 | // If writing YAML, check if atom needs a ref-name. |
| 942 | typedef MappingTraits<const lld::File*>::NormalizedFile NormalizedFile; |
| 943 | ContextInfo *info = reinterpret_cast<ContextInfo*>(io.getContext()); |
| 944 | assert(info != nullptr); |
| 945 | NormalizedFile *f = reinterpret_cast<NormalizedFile*>(info->_currentFile); |
| 946 | assert(f); |
| 947 | assert(f->_rnb); |
| 948 | if ( f->_rnb->hasRefName(atom) ) { |
| 949 | keys->_refName = f->_rnb->refName(atom); |
| 950 | } |
| 951 | } |
| 952 | |
| 953 | io.mapOptional("name", keys->_name, |
| 954 | StringRef()); |
| 955 | io.mapOptional("ref-name", keys->_refName, |
| 956 | StringRef()); |
| 957 | io.mapOptional("scope", keys->_scope, |
| 958 | lld::DefinedAtom::scopeTranslationUnit); |
| 959 | io.mapOptional("type", keys->_contentType, |
| 960 | lld::DefinedAtom::typeCode); |
| 961 | io.mapOptional("content", keys->_content); |
| 962 | io.mapOptional("size", keys->_size, |
| 963 | (uint64_t)keys->_content.size()); |
| 964 | io.mapOptional("interposable", keys->_interpose, |
| 965 | lld::DefinedAtom::interposeNo); |
| 966 | io.mapOptional("merge", keys->_merge, |
| 967 | lld::DefinedAtom::mergeNo); |
| 968 | io.mapOptional("alignment", keys->_alignment, |
| 969 | lld::DefinedAtom::Alignment(0)); |
| 970 | io.mapOptional("section-choice", keys->_sectionChoice, |
| 971 | lld::DefinedAtom::sectionBasedOnContent); |
| 972 | io.mapOptional("section-name", keys->_sectionName, |
| 973 | StringRef()); |
| 974 | io.mapOptional("dead-strip", keys->_deadStrip, |
| 975 | lld::DefinedAtom::deadStripNormal); |
Nick Kledzik | cc3d2dc | 2013-01-09 01:17:12 +0000 | [diff] [blame] | 976 | // default permissions based on content type |
| 977 | io.mapOptional("permissions", keys->_permissions, |
| 978 | lld::DefinedAtom::permissions( |
| 979 | keys->_contentType)); |
Nick Kledzik | 8a3052e | 2013-01-08 21:12:13 +0000 | [diff] [blame] | 980 | io.mapOptional("references", keys->_references); |
Nick Kledzik | 6b079f5 | 2013-01-05 02:22:35 +0000 | [diff] [blame] | 981 | } |
| 982 | }; |
| 983 | |
| 984 | |
| 985 | |
| 986 | |
Nick Kledzik | 6b079f5 | 2013-01-05 02:22:35 +0000 | [diff] [blame] | 987 | // YAML conversion for const lld::UndefinedAtom* |
| 988 | template <> |
| 989 | struct MappingTraits<const lld::UndefinedAtom*> { |
| 990 | |
| 991 | class NormalizedAtom : public lld::UndefinedAtom { |
| 992 | public: |
| 993 | NormalizedAtom(IO &io) |
| 994 | : _file(fileFromContext(io)), _name(), _canBeNull(canBeNullNever) { |
| 995 | } |
| 996 | NormalizedAtom(IO &io, const lld::UndefinedAtom *atom) |
| 997 | : _file(fileFromContext(io)), |
| 998 | _name(atom->name()), |
| 999 | _canBeNull(atom->canBeNull()) { |
| 1000 | } |
| 1001 | const lld::UndefinedAtom *denormalize(IO &io) { |
| 1002 | ContextInfo *info = reinterpret_cast<ContextInfo*>(io.getContext()); |
| 1003 | assert(info != nullptr); |
| 1004 | typedef MappingTraits<const lld::File*>::NormalizedFile NormalizedFile; |
| 1005 | NormalizedFile *f = reinterpret_cast<NormalizedFile*>(info->_currentFile); |
| 1006 | if ( !_name.empty() ) |
| 1007 | _name = f->copyString(_name); |
| 1008 | |
| 1009 | DEBUG_WITH_TYPE("WriterYAML", llvm::dbgs() |
| 1010 | << "created UndefinedAtom named: '" << _name |
| 1011 | << "' (" << (void*)_name.data() << ", " |
| 1012 | << _name.size() << ")\n"); |
| 1013 | return this; |
| 1014 | } |
| 1015 | // Extract current File object from YAML I/O parsing context |
| 1016 | const lld::File &fileFromContext(IO &io) { |
| 1017 | ContextInfo *info = reinterpret_cast<ContextInfo*>(io.getContext()); |
| 1018 | assert(info != nullptr); |
| 1019 | assert(info->_currentFile != nullptr); |
| 1020 | return *info->_currentFile; |
| 1021 | } |
| 1022 | |
| 1023 | virtual const lld::File &file() const { return _file; } |
| 1024 | virtual StringRef name() const { return _name; } |
| 1025 | virtual CanBeNull canBeNull() const { return _canBeNull; } |
| 1026 | |
| 1027 | const lld::File &_file; |
| 1028 | StringRef _name; |
| 1029 | CanBeNull _canBeNull; |
| 1030 | }; |
| 1031 | |
| 1032 | |
| 1033 | static void mapping(IO &io, const lld::UndefinedAtom* &atom) { |
| 1034 | MappingNormalizationHeap<NormalizedAtom, |
| 1035 | const lld::UndefinedAtom*> keys(io, atom); |
| 1036 | |
| 1037 | io.mapRequired("name", keys->_name); |
| 1038 | io.mapOptional("can-be-null", keys->_canBeNull, |
| 1039 | lld::UndefinedAtom::canBeNullNever); |
| 1040 | } |
| 1041 | }; |
| 1042 | |
| 1043 | |
| 1044 | // YAML conversion for const lld::SharedLibraryAtom* |
| 1045 | template <> |
| 1046 | struct MappingTraits<const lld::SharedLibraryAtom*> { |
| 1047 | |
| 1048 | class NormalizedAtom : public lld::SharedLibraryAtom { |
| 1049 | public: |
| 1050 | NormalizedAtom(IO &io) |
| 1051 | : _file(fileFromContext(io)), _name(), _loadName(), _canBeNull(false) { |
| 1052 | } |
| 1053 | NormalizedAtom(IO &io, const lld::SharedLibraryAtom *atom) |
| 1054 | : _file(fileFromContext(io)), |
| 1055 | _name(atom->name()), |
| 1056 | _loadName(atom->loadName()), |
| 1057 | _canBeNull(atom->canBeNullAtRuntime()) { |
| 1058 | } |
| 1059 | const lld::SharedLibraryAtom *denormalize(IO &io) { |
| 1060 | ContextInfo *info = reinterpret_cast<ContextInfo*>(io.getContext()); |
| 1061 | assert(info != nullptr); |
| 1062 | typedef MappingTraits<const lld::File*>::NormalizedFile NormalizedFile; |
| 1063 | NormalizedFile *f = reinterpret_cast<NormalizedFile*>(info->_currentFile); |
| 1064 | if ( !_name.empty() ) |
| 1065 | _name = f->copyString(_name); |
| 1066 | if ( !_loadName.empty() ) |
| 1067 | _loadName = f->copyString(_loadName); |
| 1068 | |
| 1069 | DEBUG_WITH_TYPE("WriterYAML", llvm::dbgs() |
| 1070 | << "created SharedLibraryAtom named: '" << _name |
| 1071 | << "' (" << (void*)_name.data() << ", " |
| 1072 | << _name.size() << ")\n"); |
| 1073 | return this; |
| 1074 | } |
| 1075 | // Extract current File object from YAML I/O parsing context |
| 1076 | const lld::File &fileFromContext(IO &io) { |
| 1077 | ContextInfo *info = reinterpret_cast<ContextInfo*>(io.getContext()); |
| 1078 | assert(info != nullptr); |
| 1079 | assert(info->_currentFile != nullptr); |
| 1080 | return *info->_currentFile; |
| 1081 | } |
| 1082 | |
| 1083 | virtual const lld::File &file() const { return _file; } |
| 1084 | virtual StringRef name() const { return _name; } |
| 1085 | virtual StringRef loadName() const { return _loadName;} |
| 1086 | virtual bool canBeNullAtRuntime() const { return _canBeNull; } |
| 1087 | |
| 1088 | const lld::File &_file; |
| 1089 | StringRef _name; |
| 1090 | StringRef _loadName; |
| 1091 | ShlibCanBeNull _canBeNull; |
| 1092 | }; |
| 1093 | |
| 1094 | |
| 1095 | static void mapping(IO &io, const lld::SharedLibraryAtom *&atom) { |
| 1096 | |
| 1097 | MappingNormalizationHeap<NormalizedAtom, |
| 1098 | const lld::SharedLibraryAtom*> keys(io, atom); |
| 1099 | |
| 1100 | io.mapRequired("name", keys->_name); |
| 1101 | io.mapOptional("load-name", keys->_loadName); |
| 1102 | io.mapOptional("can-be-null", keys->_canBeNull, |
| 1103 | (ShlibCanBeNull)false); |
| 1104 | } |
| 1105 | }; |
| 1106 | |
| 1107 | |
| 1108 | // YAML conversion for const lld::AbsoluteAtom* |
| 1109 | template <> |
| 1110 | struct MappingTraits<const lld::AbsoluteAtom*> { |
| 1111 | |
| 1112 | class NormalizedAtom : public lld::AbsoluteAtom { |
| 1113 | public: |
| 1114 | NormalizedAtom(IO &io) |
| 1115 | : _file(fileFromContext(io)), _name(), _scope(), _value(0) { |
| 1116 | } |
| 1117 | NormalizedAtom(IO &io, const lld::AbsoluteAtom *atom) |
| 1118 | : _file(fileFromContext(io)), |
| 1119 | _name(atom->name()), |
| 1120 | _scope(atom->scope()), |
| 1121 | _value(atom->value()) { |
| 1122 | } |
| 1123 | const lld::AbsoluteAtom *denormalize(IO &io) { |
| 1124 | ContextInfo *info = reinterpret_cast<ContextInfo*>(io.getContext()); |
| 1125 | assert(info != nullptr); |
| 1126 | typedef MappingTraits<const lld::File*>::NormalizedFile NormalizedFile; |
| 1127 | NormalizedFile *f = reinterpret_cast<NormalizedFile*>(info->_currentFile); |
| 1128 | if ( !_name.empty() ) |
| 1129 | _name = f->copyString(_name); |
| 1130 | |
| 1131 | DEBUG_WITH_TYPE("WriterYAML", llvm::dbgs() |
| 1132 | << "created AbsoluteAtom named: '" << _name |
| 1133 | << "' (" << (void*)_name.data() << ", " |
| 1134 | << _name.size() << ")\n"); |
| 1135 | return this; |
| 1136 | } |
| 1137 | // Extract current File object from YAML I/O parsing context |
| 1138 | const lld::File &fileFromContext(IO &io) { |
| 1139 | ContextInfo *info = reinterpret_cast<ContextInfo*>(io.getContext()); |
| 1140 | assert(info != nullptr); |
| 1141 | assert(info->_currentFile != nullptr); |
| 1142 | return *info->_currentFile; |
| 1143 | } |
| 1144 | |
| 1145 | virtual const lld::File &file() const { return _file; } |
| 1146 | virtual StringRef name() const { return _name; } |
| 1147 | virtual uint64_t value() const { return _value; } |
| 1148 | virtual Scope scope() const { return _scope; } |
| 1149 | |
| 1150 | const lld::File &_file; |
| 1151 | StringRef _name; |
| 1152 | StringRef _refName; |
| 1153 | Scope _scope; |
| 1154 | Hex64 _value; |
| 1155 | }; |
| 1156 | |
| 1157 | |
| 1158 | static void mapping(IO &io, const lld::AbsoluteAtom *&atom) { |
| 1159 | MappingNormalizationHeap<NormalizedAtom, |
| 1160 | const lld::AbsoluteAtom*> keys(io, atom); |
| 1161 | |
| 1162 | if ( io.outputting() ) { |
| 1163 | typedef MappingTraits<const lld::File*>::NormalizedFile NormalizedFile; |
| 1164 | ContextInfo *info = reinterpret_cast<ContextInfo*>(io.getContext()); |
| 1165 | assert(info != nullptr); |
| 1166 | NormalizedFile *f = reinterpret_cast<NormalizedFile*>(info->_currentFile); |
| 1167 | assert(f); |
| 1168 | assert(f->_rnb); |
| 1169 | if ( f->_rnb->hasRefName(atom) ) { |
| 1170 | keys->_refName = f->_rnb->refName(atom); |
| 1171 | } |
| 1172 | } |
| 1173 | |
| 1174 | io.mapRequired("name", keys->_name); |
| 1175 | io.mapOptional("ref-name", keys->_refName, StringRef()); |
| 1176 | io.mapOptional("scope", keys->_scope); |
| 1177 | io.mapRequired("value", keys->_value); |
| 1178 | } |
| 1179 | }; |
| 1180 | |
Nick Kledzik | bd49198 | 2013-01-08 23:51:03 +0000 | [diff] [blame] | 1181 | } // namespace llvm |
| 1182 | } // namespace yaml |
| 1183 | |
Nick Kledzik | 6b079f5 | 2013-01-05 02:22:35 +0000 | [diff] [blame] | 1184 | |
| 1185 | RefNameResolver::RefNameResolver(const lld::File *file, IO &io) : _io(io) { |
| 1186 | typedef MappingTraits<const lld::DefinedAtom*>::NormalizedAtom NormalizedAtom; |
| 1187 | for (const lld::DefinedAtom *a : file->defined() ) { |
| 1188 | NormalizedAtom *na = (NormalizedAtom*)a; |
| 1189 | if ( na->_refName.empty() ) |
| 1190 | add(na->_name, a); |
| 1191 | else |
| 1192 | add(na->_refName, a); |
| 1193 | } |
| 1194 | |
| 1195 | for (const lld::UndefinedAtom *a : file->undefined() ) |
| 1196 | add(a->name(), a); |
| 1197 | |
| 1198 | for (const lld::SharedLibraryAtom *a : file->sharedLibrary() ) |
| 1199 | add(a->name(), a); |
| 1200 | |
| 1201 | typedef MappingTraits<const lld::AbsoluteAtom*>::NormalizedAtom NormAbsAtom; |
| 1202 | for (const lld::AbsoluteAtom *a : file->absolute() ) { |
| 1203 | NormAbsAtom *na = (NormAbsAtom*)a; |
| 1204 | if ( na->_refName.empty() ) |
| 1205 | add(na->_name, a); |
| 1206 | else |
| 1207 | add(na->_refName, a); |
| 1208 | } |
| 1209 | } |
| 1210 | |
| 1211 | |
Nick Kledzik | bd49198 | 2013-01-08 23:51:03 +0000 | [diff] [blame] | 1212 | |
| 1213 | inline |
| 1214 | const lld::File* |
| 1215 | MappingTraits<const lld::File*>::NormalizedFile::denormalize(IO &io) { |
| 1216 | typedef MappingTraits<const lld::DefinedAtom*>::NormalizedAtom NormalizedAtom; |
| 1217 | |
| 1218 | RefNameResolver nameResolver(this, io); |
| 1219 | // Now that all atoms are parsed, references can be bound. |
| 1220 | for (const lld::DefinedAtom *a : this->defined() ) { |
| 1221 | NormalizedAtom *normAtom = (NormalizedAtom*)a; |
| 1222 | normAtom->bind(nameResolver); |
| 1223 | } |
| 1224 | return this; |
| 1225 | } |
| 1226 | |
| 1227 | inline |
| 1228 | void MappingTraits<const lld::DefinedAtom*>:: |
| 1229 | NormalizedAtom::bind(const RefNameResolver &resolver) { |
| 1230 | typedef MappingTraits<const lld::Reference*>::NormalizedReference |
| 1231 | NormalizedReference; |
| 1232 | for (const lld::Reference *ref : _references) { |
| 1233 | NormalizedReference *normRef = (NormalizedReference*)ref; |
| 1234 | normRef->bind(resolver); |
| 1235 | } |
| 1236 | } |
| 1237 | |
| 1238 | inline |
| 1239 | void MappingTraits<const lld::Reference*>:: |
| 1240 | NormalizedReference::bind(const RefNameResolver &resolver) { |
| 1241 | _target = resolver.lookup(_targetName); |
| 1242 | } |
| 1243 | |
| 1244 | |
Nick Kledzik | 6b079f5 | 2013-01-05 02:22:35 +0000 | [diff] [blame] | 1245 | inline |
| 1246 | llvm::StringRef MappingTraits<const lld::Reference*>::NormalizedReference:: |
| 1247 | targetName(IO &io, const lld::Reference *ref) { |
| 1248 | if ( ref->target() == nullptr ) |
| 1249 | return llvm::StringRef(); |
| 1250 | ContextInfo *info = reinterpret_cast<ContextInfo*>(io.getContext()); |
| 1251 | assert(info != nullptr); |
| 1252 | typedef MappingTraits<const lld::File*>::NormalizedFile NormalizedFile; |
| 1253 | NormalizedFile *f = reinterpret_cast<NormalizedFile*>(info->_currentFile); |
| 1254 | RefNameBuilder *rnb = f->_rnb; |
| 1255 | if ( rnb->hasRefName(ref->target()) ) |
| 1256 | return rnb->refName(ref->target()); |
| 1257 | return ref->target()->name(); |
| 1258 | } |
| 1259 | |
| 1260 | |
| 1261 | |
| 1262 | namespace lld { |
| 1263 | namespace yaml { |
| 1264 | |
| 1265 | class Writer : public lld::Writer { |
| 1266 | public: |
| 1267 | Writer(const WriterOptionsYAML &options) : _options(options) { |
| 1268 | } |
| 1269 | |
| 1270 | virtual error_code writeFile(const lld::File &file, StringRef outPath) { |
| 1271 | // Create stream to path. |
| 1272 | std::string errorInfo; |
| 1273 | llvm::raw_fd_ostream out(outPath.data(), errorInfo); |
| 1274 | if (!errorInfo.empty()) |
| 1275 | return llvm::make_error_code(llvm::errc::no_such_file_or_directory); |
| 1276 | |
| 1277 | // Create yaml Output writer, using yaml options for context. |
| 1278 | ContextInfo context(_options); |
| 1279 | llvm::yaml::Output yout(out, &context); |
| 1280 | |
| 1281 | // Write yaml output. |
| 1282 | const lld::File *fileRef = &file; |
| 1283 | yout << fileRef; |
| 1284 | |
| 1285 | return error_code::success(); |
| 1286 | } |
| 1287 | |
| 1288 | virtual StubsPass *stubPass() { |
| 1289 | return _options.stubPass(); |
| 1290 | } |
| 1291 | |
| 1292 | virtual GOTPass *gotPass() { |
| 1293 | return _options.gotPass(); |
| 1294 | } |
| 1295 | |
| 1296 | |
| 1297 | private: |
| 1298 | const WriterOptionsYAML &_options; |
| 1299 | }; |
| 1300 | |
| 1301 | |
| 1302 | |
| 1303 | class ReaderYAML : public Reader { |
| 1304 | public: |
| 1305 | ReaderYAML(const ReaderOptionsYAML &options) : _options(options) { |
| 1306 | } |
| 1307 | |
| 1308 | error_code parseFile(std::unique_ptr<MemoryBuffer> mb, |
| 1309 | std::vector<std::unique_ptr<File>> &result) { |
| 1310 | // Note: we do not take ownership of the MemoryBuffer. That is |
| 1311 | // because yaml may produce multiple File objects, so there is no |
| 1312 | // *one* File to take ownership. Therefore, the yaml File objects |
| 1313 | // produced must make copies of all strings that come from YAML I/O. |
| 1314 | // Otherwise the strings will become invalid when this MemoryBuffer |
| 1315 | // is deallocated. |
| 1316 | |
| 1317 | // Create YAML Input parser. |
| 1318 | ContextInfo context(_options); |
| 1319 | llvm::yaml::Input yin(mb->getBuffer(), &context); |
| 1320 | |
| 1321 | // Fill vector with File objects created by parsing yaml. |
| 1322 | std::vector<const lld::File*> createdFiles; |
| 1323 | yin >> createdFiles; |
| 1324 | |
| 1325 | // Quit now if there were parsing errors. |
| 1326 | if ( yin.error() ) |
| 1327 | return make_error_code(lld::yaml_reader_error::illegal_value); |
| 1328 | |
| 1329 | for (const File *file : createdFiles) { |
| 1330 | // Note: parseFile() should return vector of *const* File |
| 1331 | File *f = const_cast<File*>(file); |
| 1332 | result.emplace_back(f); |
| 1333 | } |
| 1334 | return make_error_code(lld::yaml_reader_error::success); |
| 1335 | } |
| 1336 | |
| 1337 | private: |
| 1338 | const ReaderOptionsYAML &_options; |
| 1339 | }; |
| 1340 | |
| 1341 | |
| 1342 | |
| 1343 | } // namespace yaml |
| 1344 | |
| 1345 | |
| 1346 | Writer *createWriterYAML(const WriterOptionsYAML &options) { |
| 1347 | return new lld::yaml::Writer(options); |
| 1348 | } |
| 1349 | |
| 1350 | WriterOptionsYAML::WriterOptionsYAML() { |
| 1351 | } |
| 1352 | |
| 1353 | WriterOptionsYAML::~WriterOptionsYAML() { |
| 1354 | } |
| 1355 | |
| 1356 | |
| 1357 | |
| 1358 | Reader *createReaderYAML(const ReaderOptionsYAML &options) { |
| 1359 | return new lld::yaml::ReaderYAML(options); |
| 1360 | } |
| 1361 | |
| 1362 | ReaderOptionsYAML::ReaderOptionsYAML() { |
| 1363 | } |
| 1364 | |
| 1365 | ReaderOptionsYAML::~ReaderOptionsYAML() { |
| 1366 | } |
| 1367 | |
| 1368 | |
| 1369 | } // namespace lld |
| 1370 | |