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 : // COFF relinker. Relinking can be seen as decomposing an input image,
16 : // applying a sequence of block graph transforms (some applied implicitly,
17 : // and others provided by the user), followed by a sequence of orderers
18 : // (again, some implicit, some provided by the user), laying out, and
19 : // writing a new image. CoffRelinker encapsulates this workflow.
20 : //
21 : // It is intended to be used as follows:
22 : //
23 : // CoffRelinker relinker;
24 : // relinker.set_input_path(...); // Required.
25 : // relinker.set_output_path(...); // Required.
26 : // relinker.Init(); // Check the return value!
27 : //
28 : // // At this point, the following accessors are valid:
29 : // relinker.input_image_file();
30 : // relinker.input_image_layout();
31 : // relinker.block_graph();
32 : // relinker.headers_block();
33 : //
34 : // relinker.AppendTransform(...); // May be called repeatedly.
35 : // relinker.AppendOrderer(...); // May be called repeatedly.
36 : //
37 : // relinker.Relink(); // Check the return value!
38 :
39 : #ifndef SYZYGY_PE_COFF_RELINKER_H_
40 : #define SYZYGY_PE_COFF_RELINKER_H_
41 :
42 : #include "syzygy/pe/coff_file.h"
43 : #include "syzygy/pe/coff_transform_policy.h"
44 : #include "syzygy/pe/pe_coff_relinker.h"
45 :
46 : namespace pe {
47 :
48 : // A transformation on a COFF image, decomposing an original image, applying
49 : // some transforms to it, generating the layout, and writing the new image
50 : // to disk.
51 : //
52 : // Creating a CoffRelinker and not changing its default configuration yields
53 : // an identity relinker that will produce a semantically identical image.
54 : //
55 : // The workflow is as follows:
56 : //
57 : // 1. The image is read and decomposed.
58 : // 2. The image is transformed:
59 : // a) Transforms provided by the user are applied.
60 : // d) CoffPrepareHeadersTransform is applied.
61 : // 3. The image is ordered by the user-specified orderers, or else by
62 : // OriginalOrderer if none is given.
63 : // 4. CoffImageLayoutBuilder is used to convert the OrderedBlockGraph to an
64 : // ImageLayout.
65 : // 5. The new image file is written.
66 : class CoffRelinker : public PECoffRelinker {
67 : public:
68 : // Construct a default CoffRelinker. Initialize properties to default
69 : // values.
70 : // @param transform_policy The policy that dictates how to apply transforms.
71 : explicit CoffRelinker(const CoffTransformPolicy* transform_policy);
72 :
73 : // @see RelinkerInterface::image_format()
74 E : virtual ImageFormat image_format() const override {
75 E : return BlockGraph::COFF_IMAGE;
76 E : }
77 :
78 : // Read and decompose the main input image, treated as a COFF file.
79 : //
80 : // @returns true on success, false otherwise.
81 : virtual bool Init() override;
82 :
83 : // After a successful call to Init(), apply transforms, followed by
84 : // orderers, then the resulting COFF file is written to the main output
85 : // path.
86 : //
87 : // @returns true on success, false otherwise.
88 : virtual bool Relink() override;
89 :
90 : // After a successful call to Init(), retrieve the original unmodified
91 : // COFF file reader.
92 : //
93 : // @returns the original COFF file reader.
94 E : const CoffFile& input_image_file() const { return input_image_file_; }
95 :
96 : private:
97 : // Check paths for existence and overwriting validity.
98 : //
99 : // @returns true on success, or false on failure.
100 : bool CheckPaths();
101 :
102 : // The original COFF file reader.
103 : CoffFile input_image_file_;
104 :
105 : DISALLOW_COPY_AND_ASSIGN(CoffRelinker);
106 : };
107 :
108 : } // namespace pe
109 :
110 : #endif // SYZYGY_PE_COFF_RELINKER_H_
|