1 : // Copyright 2012 Google Inc. All Rights Reserved.
2 : //
3 : // Licensed under the Apache License, Version 2.0 (the "License");
4 : // you may not use this file except in compliance with the License.
5 : // You may obtain a copy of the License at
6 : //
7 : // http://www.apache.org/licenses/LICENSE-2.0
8 : //
9 : // Unless required by applicable law or agreed to in writing, software
10 : // distributed under the License is distributed on an "AS IS" BASIS,
11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 : // See the License for the specific language governing permissions and
13 : // limitations under the License.
14 : //
15 : // Declaration of a simple metadata class, which contains toolchain information
16 : // which will be embedded in the outputs produced by the toolchain. Every
17 : // output has one thing in common: it is has been produced from or with
18 : // respect to a given module, and it has been produced by a fixed version of
19 : // the toolchain.
20 :
21 : #ifndef SYZYGY_PE_METADATA_H_
22 : #define SYZYGY_PE_METADATA_H_
23 :
24 : #include "base/logging_win.h"
25 : #include "base/time.h"
26 : #include "base/values.h"
27 : #include "syzygy/block_graph/block_graph.h"
28 : #include "syzygy/common/syzygy_version.h"
29 : #include "syzygy/core/serialization.h"
30 : #include "syzygy/pe/pe_file.h"
31 :
32 : // Forward declaration.
33 : namespace core {
34 : class JSONFileWriter;
35 : } // namespace core
36 :
37 : namespace pe {
38 :
39 : using base::Time;
40 : using block_graph::BlockGraph;
41 : using common::SyzygyVersion;
42 :
43 : // Class encapsulating the metadata that is required for traceability and
44 : // consistency at every step in the toolchain.
45 : class Metadata {
46 : public:
47 :
48 : Metadata();
49 :
50 : // Initialize this metadata for a given module. Automatically infers
51 : // command-line, time, and toolchain version from the environment. Assumes
52 : // that the singleton CommandLine has already been initialized.
53 : bool Init(const PEFile::Signature& module_signature);
54 :
55 : // Confirms the metadata is consistent with the given module and current
56 : // toolchain version.
57 : bool IsConsistent(const PEFile::Signature& module_signature) const;
58 :
59 : // Functions for serialization to and from JSON.
60 : bool SaveToJSON(core::JSONFileWriter* json_file) const;
61 : bool LoadFromJSON(const DictionaryValue& metadata);
62 :
63 : // Functions for serialization to and from a block.
64 : bool SaveToBlock(BlockGraph::Block* block) const;
65 : bool LoadFromBlock(const BlockGraph::Block* block);
66 :
67 : // Functions for serialization to and from a PE file.
68 : bool LoadFromPE(const PEFile& pe_file);
69 :
70 : // Serialization functions.
71 : bool Save(core::OutArchive* out_archive) const;
72 : bool Load(core::InArchive* in_archive);
73 :
74 : // Comparison operators for serialization testing.
75 : // @{
76 : bool operator==(const Metadata& rhs) const;
77 E : bool operator!=(const Metadata& rhs) const { return !operator==(rhs); }
78 : // @}
79 :
80 : // Accessors.
81 : const std::string& command_line() const { return command_line_; }
82 : Time creation_time() const { return creation_time_; }
83 E : const SyzygyVersion& toolchain_version() const { return toolchain_version_; }
84 E : const PEFile::Signature& module_signature() const {
85 E : return module_signature_;
86 E : }
87 :
88 : // Mutators. These are mainly for explicit testing.
89 E : void set_command_line(const std::string& command_line) {
90 E : command_line_ = command_line;
91 E : }
92 E : void set_creation_time(const Time& creation_time) {
93 E : creation_time_ = creation_time;
94 E : }
95 E : void set_toolchain_version(const SyzygyVersion& toolchain_version) {
96 E : toolchain_version_ = toolchain_version;
97 E : }
98 E : void set_module_signature(const PEFile::Signature& module_signature) {
99 E : module_signature_ = module_signature;
100 E : }
101 :
102 : private:
103 : // The command-line that was used to produce the output.
104 : std::string command_line_;
105 : // The time the output was created.
106 : Time creation_time_;
107 : // The version of the toolchain that produced the output.
108 : SyzygyVersion toolchain_version_;
109 : // The original module from/for which the output was produced.
110 : PEFile::Signature module_signature_;
111 :
112 : DISALLOW_COPY_AND_ASSIGN(Metadata);
113 : };
114 :
115 : } // namespace pe
116 :
117 : #endif // SYZYGY_PE_METADATA_H_
|