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