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 i : virtual ImageFormat image_format() const OVERRIDE { return COFF_IMAGE; }
75 :
76 : // Read and decompose the main input image, treated as a COFF file.
77 : //
78 : // @returns true on success, false otherwise.
79 : virtual bool Init() OVERRIDE;
80 :
81 : // After a successful call to Init(), apply transforms, followed by
82 : // orderers, then the resulting COFF file is written to the main output
83 : // path.
84 : //
85 : // @returns true on success, false otherwise.
86 : virtual bool Relink() OVERRIDE;
87 :
88 : // After a successful call to Init(), retrieve the original unmodified
89 : // COFF file reader.
90 : //
91 : // @returns the original COFF file reader.
92 E : const CoffFile& input_image_file() const { return input_image_file_; }
93 :
94 : private:
95 : // Check paths for existence and overwriting validity.
96 : //
97 : // @returns true on success, or false on failure.
98 : bool CheckPaths();
99 :
100 : // The original COFF file reader.
101 : CoffFile input_image_file_;
102 :
103 : DISALLOW_COPY_AND_ASSIGN(CoffRelinker);
104 : };
105 :
106 : } // namespace pe
107 :
108 : #endif // SYZYGY_PE_COFF_RELINKER_H_
|