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 : // Specialization of the instrumenter interface for instrumenters that use a
16 : // relinker. This performs all the common bits of this kind of instrumenters:
17 : // - Parse the shared command-line parameters.
18 : // - Initialization the relinker.
19 : // - Default implementation of Instrument.
20 :
21 : #ifndef SYZYGY_INSTRUMENT_INSTRUMENTERS_INSTRUMENTER_WITH_RELINKER_H_
22 : #define SYZYGY_INSTRUMENT_INSTRUMENTERS_INSTRUMENTER_WITH_RELINKER_H_
23 :
24 : #include <string>
25 :
26 : #include "base/command_line.h"
27 : #include "base/files/file_path.h"
28 : #include "syzygy/instrument/instrumenter.h"
29 : #include "syzygy/pe/coff_relinker.h"
30 : #include "syzygy/pe/pe_relinker.h"
31 :
32 : namespace instrument {
33 : namespace instrumenters {
34 :
35 : class InstrumenterWithRelinker : public InstrumenterInterface {
36 : public:
37 : typedef block_graph::BlockGraph BlockGraph;
38 : typedef block_graph::BlockGraph::ImageFormat ImageFormat;
39 :
40 : InstrumenterWithRelinker()
41 : : image_format_(BlockGraph::PE_IMAGE),
42 : allow_overwrite_(false),
43 : debug_friendly_(false),
44 : no_augment_pdb_(false),
45 E : no_strip_strings_(false) { }
46 :
47 E : ~InstrumenterWithRelinker() { }
48 :
49 : // @name InstrumenterInterface implementation.
50 : // @{
51 : bool ParseCommandLine(const base::CommandLine* command_line) final;
52 : bool Instrument() override;
53 : // @}
54 :
55 : protected:
56 : // Virtual method that determines whether or not the input object file
57 : // format is supported by the instrumenter. The default implementation
58 : // supports PE files, and does not support COFF files.
59 : virtual bool ImageFormatIsSupported(ImageFormat image_format);
60 :
61 : // Virtual method that performs quick-to-run preparation for the instrumenter,
62 : // such as parsing config files. This function is meant to be called by the
63 : // Instrument function before invoking the relinker. This allows early failure
64 : // to occur, e.g., from bad config files.
65 : virtual bool InstrumentPrepare() = 0;
66 :
67 : // Virtual method that does the actual instrumentation with the relinker.
68 : // This function is meant to be called by the Instrument function.
69 : // @note The implementation should log on failure.
70 : virtual bool InstrumentImpl() = 0;
71 :
72 : // Pure virtual method that should return the name of the instrumentation
73 : // mode.
74 : virtual const char* InstrumentationMode() = 0;
75 :
76 : // Command line parsing to be executed before all subclasses. Subclass
77 : // overrides should call Super::DoCommandLineParse() at the beginning.
78 : virtual bool DoCommandLineParse(const base::CommandLine* command_line);
79 :
80 : // Performs more validation after all parsing is done. Subclass overrides
81 : // should call Super::CheckCommandLineParse() at the end.
82 : virtual bool CheckCommandLineParse(const base::CommandLine* command_line);
83 :
84 : // @name Internal machinery, replaceable for testing purposes. These will
85 : // only ever be called once per object lifetime.
86 : // @{
87 : virtual pe::PETransformPolicy* GetPETransformPolicy();
88 : virtual pe::CoffTransformPolicy* GetCoffTransformPolicy();
89 : virtual pe::PERelinker* GetPERelinker();
90 : virtual pe::CoffRelinker* GetCoffRelinker();
91 : // @}
92 :
93 : // Creates and configures a relinker. This is split out for unittesting
94 : // purposes, allowing child classes to test their InstrumentImpl functions
95 : // in isolation.
96 : bool CreateRelinker();
97 :
98 : // The type of image file we are transforming.
99 : ImageFormat image_format_;
100 :
101 : // @name Command-line parameters.
102 : // @{
103 : base::FilePath input_image_path_;
104 : base::FilePath input_pdb_path_;
105 : base::FilePath output_image_path_;
106 : base::FilePath output_pdb_path_;
107 : bool allow_overwrite_;
108 : bool debug_friendly_;
109 : bool no_augment_pdb_;
110 : bool no_strip_strings_;
111 : // @}
112 :
113 : // This is used to save a pointer to the object returned by the call to
114 : // Get(PE|Coff)Relinker. Ownership of the object is internal in the default
115 : // case, but may be external during tests.
116 : pe::RelinkerInterface* relinker_;
117 :
118 : private:
119 : // They are used as containers for holding policy and relinker objects that
120 : // are allocated by our default Get* implementations above.
121 : scoped_ptr<block_graph::TransformPolicyInterface> policy_object_;
122 : scoped_ptr<pe::RelinkerInterface> relinker_object_;
123 :
124 : DISALLOW_COPY_AND_ASSIGN(InstrumenterWithRelinker);
125 : };
126 :
127 : } // namespace instrumenters
128 : } // namespace instrument
129 :
130 : #endif // SYZYGY_INSTRUMENT_INSTRUMENTERS_INSTRUMENTER_WITH_RELINKER_H_
|