1 : // Copyright 2013 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 : // Defines RelinkerInterface.
16 :
17 : #ifndef SYZYGY_PE_RELINKER_H_
18 : #define SYZYGY_PE_RELINKER_H_
19 :
20 : #include <vector>
21 :
22 : #include "base/logging.h"
23 : #include "syzygy/block_graph/orderer.h"
24 : #include "syzygy/block_graph/transform.h"
25 : #include "syzygy/pdb/pdb_mutator.h"
26 :
27 : namespace pe {
28 :
29 : // Interface for full file-to-file transformations of PE or COFF files.
30 : class RelinkerInterface {
31 : public:
32 : typedef block_graph::BlockGraph BlockGraph;
33 : typedef block_graph::BlockGraph::ImageFormat ImageFormat;
34 : typedef block_graph::BlockGraphOrdererInterface Orderer;
35 : typedef block_graph::BlockGraphTransformInterface BlockGraphTransform;
36 : typedef block_graph::ImageLayoutTransformInterface ImageLayoutTransform;
37 : typedef pdb::PdbMutatorInterface PdbMutator;
38 :
39 : // Virtual destructor for derived classes.
40 E : virtual ~RelinkerInterface() {}
41 :
42 : // @returns the image format handled by the relinker.
43 : virtual ImageFormat image_format() const = 0;
44 :
45 : // Add a transform to be applied. Transform objects must outlive the
46 : // relinker. Each transform will be applied in the order added to the
47 : // relinker, assuming all earlier transforms have succeeded.
48 : //
49 : // @param transform a transform to be applied.
50 : // @returns true on success, or false if adding transforms is not
51 : // supported.
52 i : virtual bool AppendTransform(BlockGraphTransform* transform) {
53 i : LOG(ERROR) << "Relinker does not support transforms.";
54 i : return false;
55 i : }
56 :
57 : // Add transforms to be applied. Transform objects must outlive the
58 : // relinker. Each transform will be applied in the order added to the
59 : // relinker, assuming all earlier transforms have succeeded.
60 : //
61 : // @param transforms transforms to be applied, in order.
62 : // @returns true on success, or false if adding transforms is not
63 : // supported.
64 : virtual bool AppendTransforms(
65 i : const std::vector<BlockGraphTransform*>& transforms) {
66 i : LOG(ERROR) << "Relinker does not support transforms.";
67 i : return false;
68 i : }
69 :
70 : // Add an orderer to be applied. Orderer objects must outlive the
71 : // relinker. Each orderer will be applied in the order added to the
72 : // relinker, assuming all earlier orderers have succeeded.
73 : //
74 : // @param orderer an orderer to be applied.
75 : // @returns true on success, or false if adding orderers is not
76 : // supported.
77 i : virtual bool AppendOrderer(Orderer* orderer) {
78 i : LOG(ERROR) << "Relinker does not support orderers.";
79 i : return false;
80 i : }
81 :
82 : // Add orderers to be applied. Orderer objects must outlive the
83 : // relinker. Each orderer will be applied in the order added to the
84 : // relinker, assuming all earlier orderers have succeeded.
85 : //
86 : // @param orderers orderers to be applied, in order.
87 : // @returns true on success, or false if adding orderers is not
88 : // supported.
89 i : virtual bool AppendOrderers(const std::vector<Orderer*>& orderers) {
90 i : LOG(ERROR) << "Relinker does not support orderers.";
91 i : return false;
92 i : }
93 :
94 : // Add a layout transform to be applied. Transform objects must outlive the
95 : // relinker. Each transform will be applied in the order added to the
96 : // relinker, assuming all earlier transforms have succeeded.
97 : //
98 : // @param transform a layout transform to be applied.
99 : // @returns true on success, or false if adding transforms is not
100 : // supported.
101 i : virtual bool AppendLayoutTransform(ImageLayoutTransform* transform) {
102 i : LOG(ERROR) << "Relinker does not support image layout transforms.";
103 i : return false;
104 i : }
105 :
106 : // Add layout transforms to be applied. Transform objects must outlive the
107 : // relinker. Each transform will be applied in the order added to the
108 : // relinker, assuming all earlier transforms have succeeded.
109 : //
110 : // @param transforms layout transforms to be applied, in order.
111 : // @returns true on success, or false if adding transforms is not
112 : // supported.
113 : virtual bool AppendLayoutTransforms(
114 i : const std::vector<ImageLayoutTransform*>& transforms) {
115 i : LOG(ERROR) << "Relinker does not support image layout transforms.";
116 i : return false;
117 i : }
118 :
119 : // Add a PDB mutator to be applied. PDB mutater objects must outlive the
120 : // relinker. Each mutator will be applied in the order added to the
121 : // relinker, assuming all earlier mutators have succeeded.
122 : //
123 : // @param pdb_mutator a PDB mutator to be applied.
124 : // @returns true on success, or false if adding PDB mutators is not
125 : // supported.
126 i : virtual bool AppendPdbMutator(PdbMutator* pdb_mutator) {
127 i : LOG(ERROR) << "Relinker does not support PDB mutators.";
128 i : return false;
129 i : }
130 :
131 : // Add PDB mutators to be applied by this relinker. Each mutator will be
132 : // applied in the order added to the relinker, assuming all earlier mutators
133 : // have succeeded.
134 : //
135 : // @param pdb_mutators a vector of mutators to be applied to the input image.
136 : // The pointers must remain valid for the lifespan of the relinker.
137 i : virtual bool AppendPdbMutators(const std::vector<PdbMutator*>& pdb_mutators) {
138 i : LOG(ERROR) << "Relinker does not support PDB mutators.";
139 i : return false;
140 i : }
141 :
142 : // Initialize the relinker from its input data.
143 : //
144 : // @returns true on success, false otherwise.
145 : virtual bool Init() = 0;
146 :
147 : // After a successful call to Init(), apply transforms, orderers, and PDB
148 : // mutators, as appropriate, then generate the output files.
149 : //
150 : // @returns true on success, false otherwise.
151 : virtual bool Relink() = 0;
152 :
153 : protected:
154 : // Protected default constructor for derived classes.
155 E : RelinkerInterface() {}
156 :
157 : private:
158 : DISALLOW_COPY_AND_ASSIGN(RelinkerInterface);
159 : };
160 :
161 : } // namespace pe
162 :
163 : #endif // SYZYGY_PE_RELINKER_H_
|