Nick Kledzik | abb6981 | 2012-05-31 22:34:00 +0000 | [diff] [blame] | 1 | //===- lib/ReaderWriter/Reader.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 | #include "lld/ReaderWriter/Reader.h" |
| 11 | |
| 12 | #include "llvm/ADT/OwningPtr.h" |
| 13 | #include "llvm/ADT/StringRef.h" |
Nick Kledzik | e555277 | 2013-12-19 21:58:00 +0000 | [diff] [blame] | 14 | #include "llvm/Support/FileUtilities.h" |
Nick Kledzik | abb6981 | 2012-05-31 22:34:00 +0000 | [diff] [blame] | 15 | #include "llvm/Support/MemoryBuffer.h" |
Nick Kledzik | e555277 | 2013-12-19 21:58:00 +0000 | [diff] [blame] | 16 | #include "llvm/Support/Path.h" |
Nick Kledzik | abb6981 | 2012-05-31 22:34:00 +0000 | [diff] [blame] | 17 | #include "llvm/Support/system_error.h" |
| 18 | |
Nick Kledzik | abb6981 | 2012-05-31 22:34:00 +0000 | [diff] [blame] | 19 | namespace lld { |
Nick Kledzik | e555277 | 2013-12-19 21:58:00 +0000 | [diff] [blame] | 20 | |
Nick Kledzik | abb6981 | 2012-05-31 22:34:00 +0000 | [diff] [blame] | 21 | Reader::~Reader() { |
| 22 | } |
Nick Kledzik | e555277 | 2013-12-19 21:58:00 +0000 | [diff] [blame] | 23 | |
Nick Kledzik | 6edd722 | 2014-01-11 01:07:43 +0000 | [diff] [blame] | 24 | |
| 25 | YamlIOTaggedDocumentHandler::~YamlIOTaggedDocumentHandler() { } |
| 26 | |
| 27 | |
Nick Kledzik | e555277 | 2013-12-19 21:58:00 +0000 | [diff] [blame] | 28 | void Registry::add(std::unique_ptr<Reader> reader) { |
| 29 | _readers.push_back(std::move(reader)); |
| 30 | } |
| 31 | |
Nick Kledzik | 6edd722 | 2014-01-11 01:07:43 +0000 | [diff] [blame] | 32 | void Registry::add(std::unique_ptr<YamlIOTaggedDocumentHandler> handler) { |
| 33 | _yamlHandlers.push_back(std::move(handler)); |
| 34 | } |
| 35 | |
Rui Ueyama | 170a1a8 | 2013-12-20 07:48:29 +0000 | [diff] [blame] | 36 | error_code |
Nick Kledzik | e555277 | 2013-12-19 21:58:00 +0000 | [diff] [blame] | 37 | Registry::parseFile(std::unique_ptr<MemoryBuffer> &mb, |
Rui Ueyama | 170a1a8 | 2013-12-20 07:48:29 +0000 | [diff] [blame] | 38 | std::vector<std::unique_ptr<File>> &result) const { |
Nick Kledzik | e555277 | 2013-12-19 21:58:00 +0000 | [diff] [blame] | 39 | // Get file type. |
| 40 | StringRef content(mb->getBufferStart(), mb->getBufferSize()); |
| 41 | llvm::sys::fs::file_magic fileType = llvm::sys::fs::identify_magic(content); |
| 42 | // Get file extension. |
| 43 | StringRef extension = llvm::sys::path::extension(mb->getBufferIdentifier()); |
| 44 | |
| 45 | // Ask each registered reader if it can handle this file type or extension. |
| 46 | for (const std::unique_ptr<Reader> &reader : _readers) { |
| 47 | if (reader->canParse(fileType, extension, *mb)) |
| 48 | return reader->parseFile(mb, *this, result); |
| 49 | } |
Rui Ueyama | 170a1a8 | 2013-12-20 07:48:29 +0000 | [diff] [blame] | 50 | |
| 51 | // No Reader could parse this file. |
Nick Kledzik | e555277 | 2013-12-19 21:58:00 +0000 | [diff] [blame] | 52 | return llvm::make_error_code(llvm::errc::executable_format_error); |
| 53 | } |
| 54 | |
| 55 | static const Registry::KindStrings kindStrings[] = { |
Nick Kledzik | f30e848 | 2013-12-20 20:34:19 +0000 | [diff] [blame] | 56 | { Reference::kindInGroup, "in-group" }, |
| 57 | { Reference::kindLayoutAfter, "layout-after" }, |
Rui Ueyama | 170a1a8 | 2013-12-20 07:48:29 +0000 | [diff] [blame] | 58 | { Reference::kindLayoutBefore, "layout-before" }, |
Nick Kledzik | e555277 | 2013-12-19 21:58:00 +0000 | [diff] [blame] | 59 | LLD_KIND_STRING_END |
| 60 | }; |
| 61 | |
| 62 | Registry::Registry() { |
Rui Ueyama | 170a1a8 | 2013-12-20 07:48:29 +0000 | [diff] [blame] | 63 | addKindTable(Reference::KindNamespace::all, Reference::KindArch::all, |
| 64 | kindStrings); |
Nick Kledzik | e555277 | 2013-12-19 21:58:00 +0000 | [diff] [blame] | 65 | } |
| 66 | |
Nick Kledzik | 6edd722 | 2014-01-11 01:07:43 +0000 | [diff] [blame] | 67 | bool Registry::handleTaggedDoc(llvm::yaml::IO &io, |
| 68 | const lld::File *&file) const { |
| 69 | for (const std::unique_ptr<YamlIOTaggedDocumentHandler> &h : _yamlHandlers) { |
| 70 | if (h->handledDocTag(io, file)) |
| 71 | return true; |
| 72 | } |
| 73 | return false; |
| 74 | } |
| 75 | |
| 76 | |
Rui Ueyama | 170a1a8 | 2013-12-20 07:48:29 +0000 | [diff] [blame] | 77 | void Registry::addKindTable(Reference::KindNamespace ns, |
Nick Kledzik | e555277 | 2013-12-19 21:58:00 +0000 | [diff] [blame] | 78 | Reference::KindArch arch, |
| 79 | const KindStrings array[]) { |
Rui Ueyama | 170a1a8 | 2013-12-20 07:48:29 +0000 | [diff] [blame] | 80 | KindEntry entry = { ns, arch, array }; |
Nick Kledzik | e555277 | 2013-12-19 21:58:00 +0000 | [diff] [blame] | 81 | _kindEntries.push_back(entry); |
| 82 | } |
| 83 | |
Rui Ueyama | 170a1a8 | 2013-12-20 07:48:29 +0000 | [diff] [blame] | 84 | bool Registry::referenceKindFromString(StringRef inputStr, |
Nick Kledzik | e555277 | 2013-12-19 21:58:00 +0000 | [diff] [blame] | 85 | Reference::KindNamespace &ns, |
Rui Ueyama | 170a1a8 | 2013-12-20 07:48:29 +0000 | [diff] [blame] | 86 | Reference::KindArch &arch, |
Nick Kledzik | e555277 | 2013-12-19 21:58:00 +0000 | [diff] [blame] | 87 | Reference::KindValue &value) const { |
| 88 | for (const KindEntry &entry : _kindEntries) { |
Rui Ueyama | 170a1a8 | 2013-12-20 07:48:29 +0000 | [diff] [blame] | 89 | for (const KindStrings *pair = entry.array; !pair->name.empty(); ++pair) { |
Nick Kledzik | e555277 | 2013-12-19 21:58:00 +0000 | [diff] [blame] | 90 | if (!inputStr.equals(pair->name)) |
| 91 | continue; |
| 92 | ns = entry.ns; |
| 93 | arch = entry.arch; |
| 94 | value = pair->value; |
| 95 | return true; |
| 96 | } |
| 97 | } |
| 98 | return false; |
| 99 | } |
| 100 | |
Rui Ueyama | 170a1a8 | 2013-12-20 07:48:29 +0000 | [diff] [blame] | 101 | bool Registry::referenceKindToString(Reference::KindNamespace ns, |
| 102 | Reference::KindArch arch, |
| 103 | Reference::KindValue value, |
Nick Kledzik | e555277 | 2013-12-19 21:58:00 +0000 | [diff] [blame] | 104 | StringRef &str) const { |
| 105 | for (const KindEntry &entry : _kindEntries) { |
| 106 | if (entry.ns != ns) |
| 107 | continue; |
| 108 | if (entry.arch != arch) |
| 109 | continue; |
Rui Ueyama | 170a1a8 | 2013-12-20 07:48:29 +0000 | [diff] [blame] | 110 | for (const KindStrings *pair = entry.array; !pair->name.empty(); ++pair) { |
Nick Kledzik | e555277 | 2013-12-19 21:58:00 +0000 | [diff] [blame] | 111 | if (pair->value != value) |
| 112 | continue; |
| 113 | str = pair->name; |
| 114 | return true; |
| 115 | } |
| 116 | } |
| 117 | return false; |
| 118 | } |
| 119 | |
Shankar Easwaran | 8962feb | 2013-03-14 16:09:49 +0000 | [diff] [blame] | 120 | } // end namespace lld |