1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 // This is the transformation and adjustment for Windows X86 executables.
7 #ifndef COURGETTE_WIN32_X86_GENERATOR_H_
8 #define COURGETTE_WIN32_X86_GENERATOR_H_
10 #include "base/logging.h"
11 #include "base/memory/scoped_ptr.h"
13 #include "courgette/assembly_program.h"
14 #include "courgette/ensemble.h"
18 class PatchGeneratorX86_32
: public TransformationPatchGenerator
{
20 PatchGeneratorX86_32(Element
* old_element
,
22 PatcherX86_32
* patcher
,
24 : TransformationPatchGenerator(old_element
, new_element
, patcher
),
28 virtual ExecutableType
Kind() { return kind_
; }
30 Status
WriteInitialParameters(SinkStream
* parameter_stream
) {
31 if (!parameter_stream
->WriteSizeVarint32(
32 old_element_
->offset_in_ensemble()) ||
33 !parameter_stream
->WriteSizeVarint32(old_element_
->region().length())) {
34 return C_STREAM_ERROR
;
37 // TODO(sra): Initialize |patcher_| with these parameters.
40 Status
PredictTransformParameters(SinkStreamSet
* prediction
) {
41 return TransformationPatchGenerator::PredictTransformParameters(prediction
);
44 Status
CorrectedTransformParameters(SinkStreamSet
* parameters
) {
45 // No code needed to write an 'empty' parameter set.
49 // The format of a transformed_element is a serialized EncodedProgram. We
50 // first disassemble the original old and new Elements into AssemblyPrograms.
51 // Then we adjust the new AssemblyProgram to make it as much like the old one
52 // as possible, before converting the AssemblyPrograms to EncodedPrograms and
54 Status
Transform(SourceStreamSet
* corrected_parameters
,
55 SinkStreamSet
* old_transformed_element
,
56 SinkStreamSet
* new_transformed_element
) {
57 // Don't expect any corrected parameters.
58 if (!corrected_parameters
->Empty())
59 return C_GENERAL_ERROR
;
61 // Generate old version of program using |corrected_parameters|.
62 // TODO(sra): refactor to use same code from patcher_.
63 AssemblyProgram
* old_program
= NULL
;
64 Status old_parse_status
=
65 ParseDetectedExecutable(old_element_
->region().start(),
66 old_element_
->region().length(),
68 if (old_parse_status
!= C_OK
) {
69 LOG(ERROR
) << "Cannot parse as Win32X86PE " << old_element_
->Name();
70 return old_parse_status
;
73 AssemblyProgram
* new_program
= NULL
;
74 Status new_parse_status
=
75 ParseDetectedExecutable(new_element_
->region().start(),
76 new_element_
->region().length(),
78 if (new_parse_status
!= C_OK
) {
79 DeleteAssemblyProgram(old_program
);
80 LOG(ERROR
) << "Cannot parse as Win32X86PE " << new_element_
->Name();
81 return new_parse_status
;
84 // Trim labels below a certain threshold
85 Status trim_old_status
= TrimLabels(old_program
);
86 if (trim_old_status
!= C_OK
) {
87 DeleteAssemblyProgram(old_program
);
88 return trim_old_status
;
91 Status trim_new_status
= TrimLabels(new_program
);
92 if (trim_new_status
!= C_OK
) {
93 DeleteAssemblyProgram(new_program
);
94 return trim_new_status
;
97 EncodedProgram
* old_encoded
= NULL
;
98 Status old_encode_status
= Encode(old_program
, &old_encoded
);
99 if (old_encode_status
!= C_OK
) {
100 DeleteAssemblyProgram(old_program
);
101 return old_encode_status
;
104 Status old_write_status
=
105 WriteEncodedProgram(old_encoded
, old_transformed_element
);
106 DeleteEncodedProgram(old_encoded
);
107 if (old_write_status
!= C_OK
) {
108 DeleteAssemblyProgram(old_program
);
109 return old_write_status
;
112 Status adjust_status
= Adjust(*old_program
, new_program
);
113 DeleteAssemblyProgram(old_program
);
114 if (adjust_status
!= C_OK
) {
115 DeleteAssemblyProgram(new_program
);
116 return adjust_status
;
119 EncodedProgram
* new_encoded
= NULL
;
120 Status new_encode_status
= Encode(new_program
, &new_encoded
);
121 DeleteAssemblyProgram(new_program
);
122 if (new_encode_status
!= C_OK
)
123 return new_encode_status
;
125 Status new_write_status
=
126 WriteEncodedProgram(new_encoded
, new_transformed_element
);
127 DeleteEncodedProgram(new_encoded
);
128 if (new_write_status
!= C_OK
)
129 return new_write_status
;
134 Status
Reform(SourceStreamSet
* transformed_element
,
135 SinkStream
* reformed_element
) {
136 return TransformationPatchGenerator::Reform(transformed_element
,
141 virtual ~PatchGeneratorX86_32() { }
143 ExecutableType kind_
;
145 DISALLOW_COPY_AND_ASSIGN(PatchGeneratorX86_32
);
148 } // namespace courgette
149 #endif // COURGETTE_WIN32_X86_GENERATOR_H_