1 //===- lib/Core/Reader.cpp ------------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "lld/Core/Reader.h"
10 #include "lld/Core/File.h"
11 #include "lld/Core/Reference.h"
12 #include "llvm/ADT/StringRef.h"
13 #include "llvm/BinaryFormat/Magic.h"
14 #include "llvm/Support/Errc.h"
15 #include "llvm/Support/FileSystem.h"
16 #include "llvm/Support/MemoryBuffer.h"
20 using llvm::file_magic;
21 using llvm::identify_magic;
25 YamlIOTaggedDocumentHandler::~YamlIOTaggedDocumentHandler() = default;
27 void Registry::add(std::unique_ptr<Reader> reader) {
28 _readers.push_back(std::move(reader));
31 void Registry::add(std::unique_ptr<YamlIOTaggedDocumentHandler> handler) {
32 _yamlHandlers.push_back(std::move(handler));
35 ErrorOr<std::unique_ptr<File>>
36 Registry::loadFile(std::unique_ptr<MemoryBuffer> mb) const {
38 StringRef content(mb->getBufferStart(), mb->getBufferSize());
39 file_magic fileType = identify_magic(content);
41 // Ask each registered reader if it can handle this file type or extension.
42 for (const std::unique_ptr<Reader> &reader : _readers) {
43 if (!reader->canParse(fileType, mb->getMemBufferRef()))
45 return reader->loadFile(std::move(mb), *this);
48 // No Reader could parse this file.
49 return make_error_code(llvm::errc::executable_format_error);
52 static const Registry::KindStrings kindStrings[] = {
53 {Reference::kindLayoutAfter, "layout-after"},
54 {Reference::kindAssociate, "associate"},
57 Registry::Registry() {
58 addKindTable(Reference::KindNamespace::all, Reference::KindArch::all,
62 bool Registry::handleTaggedDoc(llvm::yaml::IO &io,
63 const lld::File *&file) const {
64 for (const std::unique_ptr<YamlIOTaggedDocumentHandler> &h : _yamlHandlers)
65 if (h->handledDocTag(io, file))
70 void Registry::addKindTable(Reference::KindNamespace ns,
71 Reference::KindArch arch,
72 const KindStrings array[]) {
73 KindEntry entry = { ns, arch, array };
74 _kindEntries.push_back(entry);
77 bool Registry::referenceKindFromString(StringRef inputStr,
78 Reference::KindNamespace &ns,
79 Reference::KindArch &arch,
80 Reference::KindValue &value) const {
81 for (const KindEntry &entry : _kindEntries) {
82 for (const KindStrings *pair = entry.array; !pair->name.empty(); ++pair) {
83 if (!inputStr.equals(pair->name))
94 bool Registry::referenceKindToString(Reference::KindNamespace ns,
95 Reference::KindArch arch,
96 Reference::KindValue value,
97 StringRef &str) const {
98 for (const KindEntry &entry : _kindEntries) {
101 if (entry.arch != arch)
103 for (const KindStrings *pair = entry.array; !pair->name.empty(); ++pair) {
104 if (pair->value != value)
113 } // end namespace lld