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