aboutsummaryrefslogtreecommitdiff
path: root/disassembler.h
diff options
context:
space:
mode:
authorSamuel Huang <huangs@chromium.org>2018-03-13 18:19:34 +0000
committerEdward Lesmes <ehmaldonado@google.com>2021-07-23 21:50:59 +0000
commit06f1ae9aaca969ee95ef840f22b6b461c304542d (patch)
treef1e5c6624e70628e81fbf38d6cd14b974abe5d93 /disassembler.h
downloadzucchini-06f1ae9aaca969ee95ef840f22b6b461c304542d.tar.gz
[Zucchini] Move Zucchini from /chrome/installer/ to /components/.
(Use "git log --follow" to see older revisions of files). /components/ is the most logical place to put Zucchini, which only depends on /base and /testing/gtest. This move also enables Zucchini to be used by the Component Updater. Details: - Move all files; run the following to change deps and guards: sed 's/chrome\/installer/components/' *.cc *.h -i sed 's/CHROME_INSTALLER/COMPONENTS/' *.cc *.h -i - Sorting works out pretty well! - Change all 'chrome/installer/zucchini' to 'components/zucchini' throughout other parts of the repo; sort if necessary. - Fix 6 'git cl lint' errors. - Change 1 Bind() usage to BindRepeated(). - Update OWNER. Bug: 729154 Change-Id: I50c5a7d411ea85f707b5994ab319dfb2a1acccf7 Reviewed-on: https://chromium-review.googlesource.com/954923 Reviewed-by: Greg Thompson <grt@chromium.org> Reviewed-by: Jochen Eisinger <jochen@chromium.org> Reviewed-by: Samuel Huang <huangs@chromium.org> Commit-Queue: Samuel Huang <huangs@chromium.org> Cr-Commit-Position: refs/heads/master@{#542857} NOKEYCHECK=True GitOrigin-RevId: 577ef6c435e8d43be6e3e60ccbcbd1881780f4ec
Diffstat (limited to 'disassembler.h')
-rw-r--r--disassembler.h133
1 files changed, 133 insertions, 0 deletions
diff --git a/disassembler.h b/disassembler.h
new file mode 100644
index 0000000..8d41eaa
--- /dev/null
+++ b/disassembler.h
@@ -0,0 +1,133 @@
+// Copyright 2017 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef COMPONENTS_ZUCCHINI_DISASSEMBLER_H_
+#define COMPONENTS_ZUCCHINI_DISASSEMBLER_H_
+
+#include <stddef.h>
+
+#include <memory>
+#include <string>
+#include <vector>
+
+#include "base/macros.h"
+#include "components/zucchini/buffer_view.h"
+#include "components/zucchini/image_utils.h"
+
+namespace zucchini {
+
+class Disassembler;
+
+// A ReferenceGroup is associated with a specific |type| and has convenience
+// methods to obtain readers and writers for that type. A ReferenceGroup does
+// not store references; it is a lightweight class that communicates with the
+// disassembler to operate on them.
+class ReferenceGroup {
+ public:
+ // Member function pointer used to obtain a ReferenceReader.
+ using ReaderFactory = std::unique_ptr<ReferenceReader> (
+ Disassembler::*)(offset_t lower, offset_t upper);
+
+ // Member function pointer used to obtain a ReferenceWriter.
+ using WriterFactory = std::unique_ptr<ReferenceWriter> (Disassembler::*)(
+ MutableBufferView image);
+
+ ReferenceGroup() = default;
+
+ // RefinedGeneratorFactory and RefinedReceptorFactory don't have to be
+ // identical to GeneratorFactory and ReceptorFactory, but they must be
+ // convertible. As a result, they can be pointer to member function of a
+ // derived Disassembler.
+ template <class RefinedReaderFactory, class RefinedWriterFactory>
+ ReferenceGroup(ReferenceTypeTraits traits,
+ RefinedReaderFactory reader_factory,
+ RefinedWriterFactory writer_factory)
+ : traits_(traits),
+ reader_factory_(static_cast<ReaderFactory>(reader_factory)),
+ writer_factory_(static_cast<WriterFactory>(writer_factory)) {}
+
+ // Returns a reader for all references in the binary.
+ // Invalidates any other writer or reader previously obtained for |disasm|.
+ std::unique_ptr<ReferenceReader> GetReader(Disassembler* disasm) const;
+
+ // Returns a reader for references whose bytes are entirely contained in
+ // |[lower, upper)|.
+ // Invalidates any other writer or reader previously obtained for |disasm|.
+ std::unique_ptr<ReferenceReader> GetReader(offset_t lower,
+ offset_t upper,
+ Disassembler* disasm) const;
+
+ // Returns a writer for references in |image|, assuming that |image| was the
+ // same one initially parsed by |disasm|.
+ // Invalidates any other writer or reader previously obtained for |disasm|.
+ std::unique_ptr<ReferenceWriter> GetWriter(MutableBufferView image,
+ Disassembler* disasm) const;
+
+ // Returns traits describing the reference type.
+ const ReferenceTypeTraits& traits() const { return traits_; }
+
+ // Shorthand for traits().width.
+ offset_t width() const { return traits().width; }
+
+ // Shorthand for traits().type_tag.
+ TypeTag type_tag() const { return traits().type_tag; }
+
+ // Shorthand for traits().pool_tag.
+ PoolTag pool_tag() const { return traits().pool_tag; }
+
+ private:
+ ReferenceTypeTraits traits_;
+ ReaderFactory reader_factory_ = nullptr;
+ WriterFactory writer_factory_ = nullptr;
+};
+
+// A Disassembler is used to encapsulate architecture specific operations, to:
+// - Describe types of references found in the architecture using traits.
+// - Extract references contained in an image file.
+// - Correct target for some references.
+class Disassembler {
+ public:
+ // Attempts to parse |image| and create an architecture-specifc Disassembler,
+ // as determined by DIS, which is inherited from Disassembler. Returns an
+ // instance of DIS if successful, and null otherwise.
+ template <class DIS>
+ static std::unique_ptr<DIS> Make(ConstBufferView image) {
+ auto disasm = std::make_unique<DIS>();
+ if (!disasm->Parse(image))
+ return nullptr;
+ return disasm;
+ }
+
+ virtual ~Disassembler();
+
+ // Returns the type of executable handled by the Disassembler.
+ virtual ExecutableType GetExeType() const = 0;
+
+ // Returns a more detailed description of the executable type.
+ virtual std::string GetExeTypeString() const = 0;
+
+ // Creates and returns a vector that contains all groups of references.
+ // Groups must be aggregated by pool.
+ virtual std::vector<ReferenceGroup> MakeReferenceGroups() const = 0;
+
+ ConstBufferView GetImage() const { return image_; }
+ size_t size() const { return image_.size(); }
+
+ protected:
+ Disassembler();
+
+ // Parses |image| and initializes internal states. Returns true on success.
+ // This must be called once and before any other operation.
+ virtual bool Parse(ConstBufferView image) = 0;
+
+ // Raw image data. After Parse(), a Disassembler should shrink this to contain
+ // only the portion containing the executable file it recognizes.
+ ConstBufferView image_;
+
+ DISALLOW_COPY_AND_ASSIGN(Disassembler);
+};
+
+} // namespace zucchini
+
+#endif // COMPONENTS_ZUCCHINI_DISASSEMBLER_H_