1 //===- lld/Core/Reader.h - Abstract File Format Reading Interface ---------===//
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLD_CORE_READER_H
11 #define LLD_CORE_READER_H
13 #include "lld/Core/LLVM.h"
14 #include "lld/Core/Reference.h"
15 #include "llvm/Support/FileSystem.h"
16 #include "llvm/Support/YAMLTraits.h"
21 using llvm::sys::fs::file_magic;
32 class MachOLinkingContext;
34 /// \brief An abstract class for reading object files, library files, and
37 /// Each file format (e.g. mach-o, etc) has a concrete subclass of Reader.
42 /// Sniffs the file to determine if this Reader can parse it.
43 /// The method is called with:
44 /// 1) the file_magic enumeration returned by identify_magic()
45 /// 2) the whole file content buffer if the above is not enough.
46 virtual bool canParse(file_magic magic, MemoryBufferRef mb) const = 0;
48 /// \brief Parse a supplied buffer (already filled with the contents of a
49 /// file) and create a File object.
50 /// The resulting File object takes ownership of the MemoryBuffer.
51 virtual ErrorOr<std::unique_ptr<File>>
52 loadFile(std::unique_ptr<MemoryBuffer> mb, const class Registry &) const = 0;
56 /// \brief An abstract class for handling alternate yaml representations
59 /// The YAML syntax allows "tags" which are used to specify the type of
60 /// the YAML node. In lld, top level YAML documents can be in many YAML
61 /// representations (e.g mach-o encoded as yaml, etc). A tag is used to
62 /// specify which representation is used in the following YAML document.
63 /// To work, there must be a YamlIOTaggedDocumentHandler registered that
64 /// handles each tag type.
65 class YamlIOTaggedDocumentHandler {
67 virtual ~YamlIOTaggedDocumentHandler();
69 /// This method is called on each registered YamlIOTaggedDocumentHandler
70 /// until one returns true. If the subclass handles tag type !xyz, then
71 /// this method should call io.mapTag("!xzy") to see if that is the current
72 /// document type, and if so, process the rest of the document using
73 /// YAML I/O, then convert the result into an lld::File* and return it.
74 virtual bool handledDocTag(llvm::yaml::IO &io, const lld::File *&f) const = 0;
78 /// A registry to hold the list of currently registered Readers and
79 /// tables which map Reference kind values to strings.
80 /// The linker does not directly invoke Readers. Instead, it registers
81 /// Readers based on it configuration and command line options, then calls
82 /// the Registry object to parse files.
87 /// Walk the list of registered Readers and find one that can parse the
88 /// supplied file and parse it.
89 ErrorOr<std::unique_ptr<File>>
90 loadFile(std::unique_ptr<MemoryBuffer> mb) const;
92 /// Walk the list of registered kind tables to convert a Reference Kind
94 bool referenceKindFromString(StringRef inputStr, Reference::KindNamespace &ns,
95 Reference::KindArch &a,
96 Reference::KindValue &value) const;
98 /// Walk the list of registered kind tables to convert a Reference Kind
99 /// value to a string.
100 bool referenceKindToString(Reference::KindNamespace ns, Reference::KindArch a,
101 Reference::KindValue value, StringRef &) const;
103 /// Walk the list of registered tag handlers and have the one that handles
104 /// the current document type process the yaml into an lld::File*.
105 bool handleTaggedDoc(llvm::yaml::IO &io, const lld::File *&file) const;
107 // These methods are called to dynamically add support for various file
108 // formats. The methods are also implemented in the appropriate lib*.a
109 // library, so that the code for handling a format is only linked in, if this
110 // method is used. Any options that a Reader might need must be passed
111 // as parameters to the addSupport*() method.
112 void addSupportArchives(bool logLoading);
113 void addSupportYamlFiles();
114 void addSupportMachOObjects(MachOLinkingContext &);
116 /// To convert between kind values and names, the registry walks the list
117 /// of registered kind tables. Each table is a zero terminated array of
118 /// KindStrings elements.
120 Reference::KindValue value;
124 /// A Reference Kind value is a tuple of <namespace, arch, value>. All
125 /// entries in a conversion table have the same <namespace, arch>. The
126 /// array then contains the value/name pairs.
127 void addKindTable(Reference::KindNamespace ns, Reference::KindArch arch,
128 const KindStrings array[]);
133 Reference::KindNamespace ns;
134 Reference::KindArch arch;
135 const KindStrings *array;
138 void add(std::unique_ptr<Reader>);
139 void add(std::unique_ptr<YamlIOTaggedDocumentHandler>);
141 std::vector<std::unique_ptr<Reader>> _readers;
142 std::vector<std::unique_ptr<YamlIOTaggedDocumentHandler>> _yamlHandlers;
143 std::vector<KindEntry> _kindEntries;
146 // Utilities for building a KindString table. For instance:
147 // static const Registry::KindStrings table[] = {
148 // LLD_KIND_STRING_ENTRY(R_VAX_ADDR16),
149 // LLD_KIND_STRING_ENTRY(R_VAX_DATA16),
150 // LLD_KIND_STRING_END
152 #define LLD_KIND_STRING_ENTRY(name) { name, #name }
153 #define LLD_KIND_STRING_END { 0, "" }
155 } // end namespace lld