1 : // Copyright 2015 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 : // A BlockGraph transform that saves a hot patching metadata stream (.syzyhp)
16 : // that contains the locations and sizes of the blocks that have been prepared
17 : // for hot patching.
18 : //
19 : // Before using this transform, one should prepare blocks for hot patching
20 : // using the PEHotPatchingBasicBlockTransfrom and use the set_blocks_prepared
21 : // method to pass the vector of prepared blocks.
22 :
23 : #ifndef SYZYGY_PE_TRANSFORMS_ADD_HOT_PATCHING_METADATA_TRANSFORM_H_
24 : #define SYZYGY_PE_TRANSFORMS_ADD_HOT_PATCHING_METADATA_TRANSFORM_H_
25 :
26 : #include <vector>
27 :
28 : #include "base/callback.h"
29 : #include "syzygy/block_graph/transforms/named_transform.h"
30 :
31 : namespace pe {
32 : namespace transforms {
33 :
34 : class AddHotPatchingMetadataTransform
35 : : public block_graph::transforms::NamedBlockGraphTransformImpl<
36 : AddHotPatchingMetadataTransform> {
37 : public:
38 : typedef block_graph::BlockGraph BlockGraph;
39 : typedef block_graph::TransformPolicyInterface TransformPolicyInterface;
40 : typedef std::vector<BlockGraph::Block*> BlockVector;
41 :
42 : // The transform name.
43 : static const char kTransformName[];
44 :
45 : AddHotPatchingMetadataTransform();
46 :
47 : // @name NamedBlockGraphTransformImpl implementation.
48 : // @{
49 : // Add the metadata stream to the BlockGraph.
50 : // @param policy The policy object restricting how the transform is applied.
51 : // @param block_graph the block graph being transformed.
52 : // @param header_block the header block.
53 : // @returns true on success, false otherwise.
54 : bool TransformBlockGraph(const TransformPolicyInterface* policy,
55 : BlockGraph* block_graph,
56 : BlockGraph::Block* header_block);
57 : // @}
58 :
59 : // @name Accessors.
60 : // @{
61 : // Before using this transform, one should pass a pointer to a vector of
62 : // blocks that have been prepared for hot patching to this function.
63 : // @param blocks_prepared The vector that contains the prepared blocks.
64 E : void set_blocks_prepared(const BlockVector* blocks_prepared) {
65 E : blocks_prepared_ = blocks_prepared;
66 E : }
67 : // Retrieves the pointer to vector of blocks that have been prepared for
68 : // hot patching.
69 : // @returns the pointer to the vector of blocks.
70 E : const BlockVector* blocks_prepared() {
71 E : return blocks_prepared_;
72 E : }
73 : // @}
74 :
75 : protected:
76 : // Adds a section containing the hot patching metadata.
77 : // @param block_graph The block_graph to modify.
78 : void AddHotPatchingSection(BlockGraph* block_graph);
79 :
80 : // Calculates the code size of a block. It assumes that everything before the
81 : // first DATA_LABEL is code. If the block contains no data labels, the whole
82 : // data of the block is considered to be code.
83 : // @param block The block to calculate
84 : // @returns the size of the code the block contains.
85 : static size_t CalculateCodeSize(const BlockGraph::Block* block);
86 :
87 : private:
88 : // This is a pointer to a vector that contains the blocks prepared for
89 : // hot patching. The TransformBlockGraph uses this data to build the
90 : // hot patching stream.
91 : const BlockVector* blocks_prepared_;
92 :
93 : DISALLOW_COPY_AND_ASSIGN(AddHotPatchingMetadataTransform);
94 : };
95 :
96 : } // namespace transforms
97 : } // namespace pe
98 :
99 : #endif // SYZYGY_PE_TRANSFORMS_ADD_HOT_PATCHING_METADATA_TRANSFORM_H_
|