annotate lld/lib/Core/Reader.cpp @ 222:81f6424ef0e3 llvm-original

LLVM original branch
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Sun, 18 Jul 2021 22:10:01 +0900
parents 1d019706d866
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
150
anatofuz
parents:
diff changeset
1 //===- lib/Core/Reader.cpp ------------------------------------------------===//
anatofuz
parents:
diff changeset
2 //
anatofuz
parents:
diff changeset
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
anatofuz
parents:
diff changeset
4 // See https://llvm.org/LICENSE.txt for license information.
anatofuz
parents:
diff changeset
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
anatofuz
parents:
diff changeset
6 //
anatofuz
parents:
diff changeset
7 //===----------------------------------------------------------------------===//
anatofuz
parents:
diff changeset
8
anatofuz
parents:
diff changeset
9 #include "lld/Core/Reader.h"
anatofuz
parents:
diff changeset
10 #include "lld/Core/File.h"
anatofuz
parents:
diff changeset
11 #include "lld/Core/Reference.h"
anatofuz
parents:
diff changeset
12 #include "llvm/ADT/StringRef.h"
anatofuz
parents:
diff changeset
13 #include "llvm/BinaryFormat/Magic.h"
anatofuz
parents:
diff changeset
14 #include "llvm/Support/Errc.h"
anatofuz
parents:
diff changeset
15 #include "llvm/Support/FileSystem.h"
anatofuz
parents:
diff changeset
16 #include "llvm/Support/MemoryBuffer.h"
anatofuz
parents:
diff changeset
17 #include <algorithm>
anatofuz
parents:
diff changeset
18 #include <memory>
anatofuz
parents:
diff changeset
19
anatofuz
parents:
diff changeset
20 using llvm::file_magic;
anatofuz
parents:
diff changeset
21 using llvm::identify_magic;
anatofuz
parents:
diff changeset
22
anatofuz
parents:
diff changeset
23 namespace lld {
anatofuz
parents:
diff changeset
24
anatofuz
parents:
diff changeset
25 YamlIOTaggedDocumentHandler::~YamlIOTaggedDocumentHandler() = default;
anatofuz
parents:
diff changeset
26
anatofuz
parents:
diff changeset
27 void Registry::add(std::unique_ptr<Reader> reader) {
anatofuz
parents:
diff changeset
28 _readers.push_back(std::move(reader));
anatofuz
parents:
diff changeset
29 }
anatofuz
parents:
diff changeset
30
anatofuz
parents:
diff changeset
31 void Registry::add(std::unique_ptr<YamlIOTaggedDocumentHandler> handler) {
anatofuz
parents:
diff changeset
32 _yamlHandlers.push_back(std::move(handler));
anatofuz
parents:
diff changeset
33 }
anatofuz
parents:
diff changeset
34
anatofuz
parents:
diff changeset
35 ErrorOr<std::unique_ptr<File>>
anatofuz
parents:
diff changeset
36 Registry::loadFile(std::unique_ptr<MemoryBuffer> mb) const {
anatofuz
parents:
diff changeset
37 // Get file magic.
anatofuz
parents:
diff changeset
38 StringRef content(mb->getBufferStart(), mb->getBufferSize());
anatofuz
parents:
diff changeset
39 file_magic fileType = identify_magic(content);
anatofuz
parents:
diff changeset
40
anatofuz
parents:
diff changeset
41 // Ask each registered reader if it can handle this file type or extension.
anatofuz
parents:
diff changeset
42 for (const std::unique_ptr<Reader> &reader : _readers) {
anatofuz
parents:
diff changeset
43 if (!reader->canParse(fileType, mb->getMemBufferRef()))
anatofuz
parents:
diff changeset
44 continue;
anatofuz
parents:
diff changeset
45 return reader->loadFile(std::move(mb), *this);
anatofuz
parents:
diff changeset
46 }
anatofuz
parents:
diff changeset
47
anatofuz
parents:
diff changeset
48 // No Reader could parse this file.
anatofuz
parents:
diff changeset
49 return make_error_code(llvm::errc::executable_format_error);
anatofuz
parents:
diff changeset
50 }
anatofuz
parents:
diff changeset
51
anatofuz
parents:
diff changeset
52 static const Registry::KindStrings kindStrings[] = {
anatofuz
parents:
diff changeset
53 {Reference::kindLayoutAfter, "layout-after"},
anatofuz
parents:
diff changeset
54 {Reference::kindAssociate, "associate"},
anatofuz
parents:
diff changeset
55 LLD_KIND_STRING_END};
anatofuz
parents:
diff changeset
56
anatofuz
parents:
diff changeset
57 Registry::Registry() {
anatofuz
parents:
diff changeset
58 addKindTable(Reference::KindNamespace::all, Reference::KindArch::all,
anatofuz
parents:
diff changeset
59 kindStrings);
anatofuz
parents:
diff changeset
60 }
anatofuz
parents:
diff changeset
61
anatofuz
parents:
diff changeset
62 bool Registry::handleTaggedDoc(llvm::yaml::IO &io,
anatofuz
parents:
diff changeset
63 const lld::File *&file) const {
anatofuz
parents:
diff changeset
64 for (const std::unique_ptr<YamlIOTaggedDocumentHandler> &h : _yamlHandlers)
anatofuz
parents:
diff changeset
65 if (h->handledDocTag(io, file))
anatofuz
parents:
diff changeset
66 return true;
anatofuz
parents:
diff changeset
67 return false;
anatofuz
parents:
diff changeset
68 }
anatofuz
parents:
diff changeset
69
anatofuz
parents:
diff changeset
70 void Registry::addKindTable(Reference::KindNamespace ns,
anatofuz
parents:
diff changeset
71 Reference::KindArch arch,
anatofuz
parents:
diff changeset
72 const KindStrings array[]) {
anatofuz
parents:
diff changeset
73 KindEntry entry = { ns, arch, array };
anatofuz
parents:
diff changeset
74 _kindEntries.push_back(entry);
anatofuz
parents:
diff changeset
75 }
anatofuz
parents:
diff changeset
76
anatofuz
parents:
diff changeset
77 bool Registry::referenceKindFromString(StringRef inputStr,
anatofuz
parents:
diff changeset
78 Reference::KindNamespace &ns,
anatofuz
parents:
diff changeset
79 Reference::KindArch &arch,
anatofuz
parents:
diff changeset
80 Reference::KindValue &value) const {
anatofuz
parents:
diff changeset
81 for (const KindEntry &entry : _kindEntries) {
anatofuz
parents:
diff changeset
82 for (const KindStrings *pair = entry.array; !pair->name.empty(); ++pair) {
anatofuz
parents:
diff changeset
83 if (!inputStr.equals(pair->name))
anatofuz
parents:
diff changeset
84 continue;
anatofuz
parents:
diff changeset
85 ns = entry.ns;
anatofuz
parents:
diff changeset
86 arch = entry.arch;
anatofuz
parents:
diff changeset
87 value = pair->value;
anatofuz
parents:
diff changeset
88 return true;
anatofuz
parents:
diff changeset
89 }
anatofuz
parents:
diff changeset
90 }
anatofuz
parents:
diff changeset
91 return false;
anatofuz
parents:
diff changeset
92 }
anatofuz
parents:
diff changeset
93
anatofuz
parents:
diff changeset
94 bool Registry::referenceKindToString(Reference::KindNamespace ns,
anatofuz
parents:
diff changeset
95 Reference::KindArch arch,
anatofuz
parents:
diff changeset
96 Reference::KindValue value,
anatofuz
parents:
diff changeset
97 StringRef &str) const {
anatofuz
parents:
diff changeset
98 for (const KindEntry &entry : _kindEntries) {
anatofuz
parents:
diff changeset
99 if (entry.ns != ns)
anatofuz
parents:
diff changeset
100 continue;
anatofuz
parents:
diff changeset
101 if (entry.arch != arch)
anatofuz
parents:
diff changeset
102 continue;
anatofuz
parents:
diff changeset
103 for (const KindStrings *pair = entry.array; !pair->name.empty(); ++pair) {
anatofuz
parents:
diff changeset
104 if (pair->value != value)
anatofuz
parents:
diff changeset
105 continue;
anatofuz
parents:
diff changeset
106 str = pair->name;
anatofuz
parents:
diff changeset
107 return true;
anatofuz
parents:
diff changeset
108 }
anatofuz
parents:
diff changeset
109 }
anatofuz
parents:
diff changeset
110 return false;
anatofuz
parents:
diff changeset
111 }
anatofuz
parents:
diff changeset
112
anatofuz
parents:
diff changeset
113 } // end namespace lld