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/time.h"
25 : #include "base/values.h"
26 : #include "syzygy/block_graph/block_graph.h"
27 : #include "syzygy/common/syzygy_version.h"
28 : #include "syzygy/core/serialization.h"
29 : #include "syzygy/pe/pe_file.h"
30 :
31 : // Forward declaration.
32 : namespace core {
33 : class JSONFileWriter;
34 : } // namespace core
35 :
36 : namespace pe {
37 :
38 : using base::Time;
39 : using block_graph::BlockGraph;
40 : using common::SyzygyVersion;
41 :
42 : // Class encapsulating the metadata that is required for traceability and
43 : // consistency at every step in the toolchain.
44 : class Metadata {
45 : public:
46 :
47 : Metadata();
48 :
49 : // Initialize this metadata for a given module. Automatically infers
50 : // command-line, time, and toolchain version from the environment. Assumes
51 : // that the singleton CommandLine has already been initialized.
52 : bool Init(const PEFile::Signature& module_signature);
53 :
54 : // Confirms the metadata is consistent with the given module and current
55 : // toolchain version.
56 : bool IsConsistent(const PEFile::Signature& module_signature) const;
57 :
58 : // Functions for serialization to and from JSON.
59 : bool SaveToJSON(core::JSONFileWriter* json_file) const;
60 : bool LoadFromJSON(const DictionaryValue& metadata);
61 :
62 : // Functions for serialization to and from a block.
63 : bool SaveToBlock(BlockGraph::Block* block) const;
64 : bool LoadFromBlock(const BlockGraph::Block* block);
65 :
66 : // Functions for serialization to and from a PE file.
67 : bool LoadFromPE(const PEFile& pe_file);
68 :
69 : // Serialization functions.
70 : bool Save(core::OutArchive* out_archive) const;
71 : bool Load(core::InArchive* in_archive);
72 :
73 : // Comparison operators for serialization testing.
74 : // @{
75 : bool operator==(const Metadata& rhs) const;
76 E : bool operator!=(const Metadata& rhs) const { return !operator==(rhs); }
77 : // @}
78 :
79 : // Accessors.
80 : const std::string& command_line() const { return command_line_; }
81 : Time creation_time() const { return creation_time_; }
82 E : const SyzygyVersion& toolchain_version() const { return toolchain_version_; }
83 E : const PEFile::Signature& module_signature() const {
84 E : return module_signature_;
85 E : }
86 :
87 : // Mutators. These are mainly for explicit testing.
88 E : void set_command_line(const std::string& command_line) {
89 E : command_line_ = command_line;
90 E : }
91 E : void set_creation_time(const Time& creation_time) {
92 E : creation_time_ = creation_time;
93 E : }
94 E : void set_toolchain_version(const SyzygyVersion& toolchain_version) {
95 E : toolchain_version_ = toolchain_version;
96 E : }
97 E : void set_module_signature(const PEFile::Signature& module_signature) {
98 E : module_signature_ = module_signature;
99 E : }
100 :
101 : private:
102 : // The command-line that was used to produce the output.
103 : std::string command_line_;
104 : // The time the output was created.
105 : Time creation_time_;
106 : // The version of the toolchain that produced the output.
107 : SyzygyVersion toolchain_version_;
108 : // The original module from/for which the output was produced.
109 : PEFile::Signature module_signature_;
110 :
111 : DISALLOW_COPY_AND_ASSIGN(Metadata);
112 : };
113 :
114 : } // namespace pe
115 :
116 : #endif // SYZYGY_PE_METADATA_H_
|