diff options
Diffstat (limited to 'lib/ReaderWriter/YAML/ReaderWriterYAML.cpp')
| -rw-r--r-- | lib/ReaderWriter/YAML/ReaderWriterYAML.cpp | 137 |
1 files changed, 63 insertions, 74 deletions
diff --git a/lib/ReaderWriter/YAML/ReaderWriterYAML.cpp b/lib/ReaderWriter/YAML/ReaderWriterYAML.cpp index 868b9497c4cc6..78c6797b713f3 100644 --- a/lib/ReaderWriter/YAML/ReaderWriterYAML.cpp +++ b/lib/ReaderWriter/YAML/ReaderWriterYAML.cpp @@ -25,6 +25,7 @@ #include "llvm/Support/ErrorHandling.h" #include "llvm/Support/Format.h" #include "llvm/Support/MemoryBuffer.h" +#include "llvm/Support/Path.h" #include "llvm/Support/YAMLTraits.h" #include "llvm/Support/raw_ostream.h" #include <memory> @@ -214,30 +215,14 @@ private: NameToAtom _groupMap; }; -// Used in NormalizedFile to hold the atoms lists. -template <typename T> class AtomList : public lld::File::atom_collection<T> { +/// Mapping of Atoms. +template <typename T> class AtomList { + typedef lld::File::AtomVector<T> Ty; + public: - virtual lld::File::atom_iterator<T> begin() const { - return lld::File::atom_iterator<T>( - *this, - _atoms.empty() ? 0 : reinterpret_cast<const void *>(_atoms.data())); - } - virtual lld::File::atom_iterator<T> end() const { - return lld::File::atom_iterator<T>( - *this, _atoms.empty() ? 0 : reinterpret_cast<const void *>( - _atoms.data() + _atoms.size())); - } - virtual const T *deref(const void *it) const { - return *reinterpret_cast<const T *const *>(it); - } - virtual void next(const void *&it) const { - const T *const *p = reinterpret_cast<const T *const *>(it); - ++p; - it = reinterpret_cast<const void *>(p); - } - virtual void push_back(const T *element) { _atoms.push_back(element); } - virtual uint64_t size() const { return _atoms.size(); } - std::vector<const T *> _atoms; + typename Ty::iterator begin() { return _atoms.begin(); } + typename Ty::iterator end() { return _atoms.end(); } + Ty _atoms; }; /// Mapping of kind: field in yaml files. @@ -270,7 +255,7 @@ struct RefKind { Reference::KindValue value; }; -} // namespace anon +} // anonymous namespace LLVM_YAML_IS_SEQUENCE_VECTOR(ArchMember) LLVM_YAML_IS_SEQUENCE_VECTOR(const lld::Reference *) @@ -377,6 +362,9 @@ template <> struct ScalarEnumerationTraits<lld::DefinedAtom::CodeModel> { io.enumCase(value, "mips-micro-pic", lld::DefinedAtom::codeMipsMicroPIC); io.enumCase(value, "mips-16", lld::DefinedAtom::codeMips16); io.enumCase(value, "arm-thumb", lld::DefinedAtom::codeARMThumb); + io.enumCase(value, "arm-a", lld::DefinedAtom::codeARM_a); + io.enumCase(value, "arm-d", lld::DefinedAtom::codeARM_d); + io.enumCase(value, "arm-t", lld::DefinedAtom::codeARM_t); } }; @@ -449,6 +437,7 @@ template <> struct ScalarEnumerationTraits<lld::DefinedAtom::ContentType> { io.enumCase(value, "no-alloc", DefinedAtom::typeNoAlloc); io.enumCase(value, "group-comdat", DefinedAtom::typeGroupComdat); io.enumCase(value, "gnu-linkonce", DefinedAtom::typeGnuLinkOnce); + io.enumCase(value, "sectcreate", DefinedAtom::typeSectCreate); } }; @@ -478,15 +467,15 @@ struct ScalarEnumerationTraits<lld::SharedLibraryAtom::Type> { /// This is a custom formatter for lld::DefinedAtom::Alignment. Values look /// like: -/// 2^3 # 8-byte aligned -/// 7 mod 2^4 # 16-byte aligned plus 7 bytes +/// 8 # 8-byte aligned +/// 7 mod 16 # 16-byte aligned plus 7 bytes template <> struct ScalarTraits<lld::DefinedAtom::Alignment> { static void output(const lld::DefinedAtom::Alignment &value, void *ctxt, raw_ostream &out) { if (value.modulus == 0) { - out << llvm::format("2^%d", value.powerOf2); + out << llvm::format("%d", value.value); } else { - out << llvm::format("%d mod 2^%d", value.modulus, value.powerOf2); + out << llvm::format("%d mod %d", value.modulus, value.value); } } @@ -505,16 +494,12 @@ template <> struct ScalarTraits<lld::DefinedAtom::Alignment> { scalar = scalar.drop_front(modStart + 3); scalar = scalar.ltrim(); } - if (!scalar.startswith("2^")) { - return "malformed alignment"; - } - StringRef powerStr = scalar.drop_front(2); unsigned int power; - if (powerStr.getAsInteger(0, power)) { + if (scalar.getAsInteger(0, power)) { return "malformed alignment power"; } - value.powerOf2 = power; - if (value.modulus > (1 << value.powerOf2)) { + value.value = power; + if (value.modulus >= power) { return "malformed alignment, modulus too large for power"; } return StringRef(); // returning empty string means success @@ -598,19 +583,23 @@ template <> struct MappingTraits<const lld::File *> { const lld::File *denormalize(IO &io) { return this; } - const atom_collection<lld::DefinedAtom> &defined() const override { + const AtomVector<lld::DefinedAtom> &defined() const override { return _noDefinedAtoms; } - const atom_collection<lld::UndefinedAtom> &undefined() const override { + + const AtomVector<lld::UndefinedAtom> &undefined() const override { return _noUndefinedAtoms; } - virtual const atom_collection<lld::SharedLibraryAtom> & + + const AtomVector<lld::SharedLibraryAtom> & sharedLibrary() const override { return _noSharedLibraryAtoms; } - const atom_collection<lld::AbsoluteAtom> &absolute() const override { + + const AtomVector<lld::AbsoluteAtom> &absolute() const override { return _noAbsoluteAtoms; } + File *find(StringRef name, bool dataSymbolOnly) override { for (const ArchMember &member : _members) { for (const lld::DefinedAtom *atom : member._content->defined()) { @@ -630,7 +619,7 @@ template <> struct MappingTraits<const lld::File *> { return nullptr; } - virtual std::error_code + std::error_code parseAllMembers(std::vector<std::unique_ptr<File>> &result) override { return std::error_code(); } @@ -646,28 +635,31 @@ template <> struct MappingTraits<const lld::File *> { : File(file->path(), kindObject), _io(io), _rnb(new RefNameBuilder(*file)), _path(file->path()) { for (const lld::DefinedAtom *a : file->defined()) - _definedAtoms.push_back(a); + _definedAtoms._atoms.push_back(a); for (const lld::UndefinedAtom *a : file->undefined()) - _undefinedAtoms.push_back(a); + _undefinedAtoms._atoms.push_back(a); for (const lld::SharedLibraryAtom *a : file->sharedLibrary()) - _sharedLibraryAtoms.push_back(a); + _sharedLibraryAtoms._atoms.push_back(a); for (const lld::AbsoluteAtom *a : file->absolute()) - _absoluteAtoms.push_back(a); + _absoluteAtoms._atoms.push_back(a); } const lld::File *denormalize(IO &io); - const atom_collection<lld::DefinedAtom> &defined() const override { - return _definedAtoms; + const AtomVector<lld::DefinedAtom> &defined() const override { + return _definedAtoms._atoms; } - const atom_collection<lld::UndefinedAtom> &undefined() const override { - return _undefinedAtoms; + + const AtomVector<lld::UndefinedAtom> &undefined() const override { + return _undefinedAtoms._atoms; } - virtual const atom_collection<lld::SharedLibraryAtom> & + + const AtomVector<lld::SharedLibraryAtom> & sharedLibrary() const override { - return _sharedLibraryAtoms; + return _sharedLibraryAtoms._atoms; } - const atom_collection<lld::AbsoluteAtom> &absolute() const override { - return _absoluteAtoms; + + const AtomVector<lld::AbsoluteAtom> &absolute() const override { + return _absoluteAtoms._atoms; } // Allocate a new copy of this string in _storage, so the strings @@ -795,7 +787,7 @@ template <> struct MappingTraits<const lld::DefinedAtom *> { public: NormalizedAtom(IO &io) : _file(fileFromContext(io)), _name(), _refName(), _contentType(), - _alignment(0), _content(), _references(), _isGroupChild(false) { + _alignment(1), _content(), _references(), _isGroupChild(false) { static uint32_t ordinalCounter = 1; _ordinal = ordinalCounter++; } @@ -941,7 +933,7 @@ template <> struct MappingTraits<const lld::DefinedAtom *> { DefinedAtom::interposeNo); io.mapOptional("merge", keys->_merge, DefinedAtom::mergeNo); io.mapOptional("alignment", keys->_alignment, - DefinedAtom::Alignment(0)); + DefinedAtom::Alignment(1)); io.mapOptional("section-choice", keys->_sectionChoice, DefinedAtom::sectionBasedOnContent); io.mapOptional("section-name", keys->_sectionName, StringRef()); @@ -1246,7 +1238,7 @@ namespace yaml { class Writer : public lld::Writer { public: - Writer(const LinkingContext &context) : _context(context) {} + Writer(const LinkingContext &context) : _ctx(context) {} std::error_code writeFile(const lld::File &file, StringRef outPath) override { // Create stream to path. @@ -1257,8 +1249,8 @@ public: // Create yaml Output writer, using yaml options for context. YamlContext yamlContext; - yamlContext._linkingContext = &_context; - yamlContext._registry = &_context.registry(); + yamlContext._ctx = &_ctx; + yamlContext._registry = &_ctx.registry(); llvm::yaml::Output yout(out, &yamlContext); // Write yaml output. @@ -1269,7 +1261,7 @@ public: } private: - const LinkingContext &_context; + const LinkingContext &_ctx; }; } // end namespace yaml @@ -1287,7 +1279,6 @@ class NativeYamlIOTaggedDocumentHandler : public YamlIOTaggedDocumentHandler { } }; - /// Handles !archive tagged yaml documents. class ArchiveYamlIOTaggedDocumentHandler : public YamlIOTaggedDocumentHandler { bool handledDocTag(llvm::yaml::IO &io, const lld::File *&file) const override { @@ -1299,19 +1290,18 @@ class ArchiveYamlIOTaggedDocumentHandler : public YamlIOTaggedDocumentHandler { } }; - - class YAMLReader : public Reader { public: YAMLReader(const Registry ®istry) : _registry(registry) {} - bool canParse(file_magic, StringRef ext, const MemoryBuffer &) const override { - return (ext.equals(".objtxt") || ext.equals(".yaml")); + bool canParse(file_magic magic, MemoryBufferRef mb) const override { + StringRef name = mb.getBufferIdentifier(); + return name.endswith(".objtxt") || name.endswith(".yaml"); } - std::error_code - loadFile(std::unique_ptr<MemoryBuffer> mb, const class Registry &, - std::vector<std::unique_ptr<File>> &result) const override { + ErrorOr<std::unique_ptr<File>> + loadFile(std::unique_ptr<MemoryBuffer> mb, + const class Registry &) const override { // Create YAML Input Reader. YamlContext yamlContext; yamlContext._registry = &_registry; @@ -1321,20 +1311,19 @@ public: // Fill vector with File objects created by parsing yaml. std::vector<const lld::File *> createdFiles; yin >> createdFiles; + assert(createdFiles.size() == 1); // Error out now if there were parsing errors. if (yin.error()) return make_error_code(lld::YamlReaderError::illegal_value); std::shared_ptr<MemoryBuffer> smb(mb.release()); - for (const File *file : createdFiles) { - // Note: loadFile() should return vector of *const* File - File *f = const_cast<File *>(file); - f->setLastError(std::error_code()); - f->setSharedMemoryBuffer(smb); - result.emplace_back(f); - } - return make_error_code(lld::YamlReaderError::success); + const File *file = createdFiles[0]; + // Note: loadFile() should return vector of *const* File + File *f = const_cast<File *>(file); + f->setLastError(std::error_code()); + f->setSharedMemoryBuffer(smb); + return std::unique_ptr<File>(f); } private: |
