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