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 : // Declares utilities that are common in the decomposing, transforming,
16 : // ordering, laying out and writing of a PE image file. These utilities
17 : // constitute the core tasks performed by the PERelinker.
18 :
19 : #ifndef SYZYGY_PE_PE_RELINKER_UTIL_H_
20 : #define SYZYGY_PE_PE_RELINKER_UTIL_H_
21 :
22 : #include "base/files/file_path.h"
23 : #include "syzygy/block_graph/ordered_block_graph.h"
24 : #include "syzygy/pdb/pdb_file.h"
25 : #include "syzygy/pe/image_layout.h"
26 : #include "syzygy/pe/image_source_map.h"
27 : #include "syzygy/pe/pe_transform_policy.h"
28 :
29 m : namespace pe {
30 :
31 : // Validates input and output module paths, and infers/validates input and
32 : // output PDB paths. Logs an error on failure.
33 : // @param input_module The path to the input module.
34 : // @param output_module The path to the output module.
35 : // @param allow_overwrite If true then this won't check to ensure that the
36 : // output paths already exist.
37 : // @param input_pdb The path to the input PDB. This may be empty, in which
38 : // case it will be automatically determined.
39 : // @param output_pdb The path to the output PDB. This may be empty, in
40 : // which case it will be automatically determined.
41 : // @returns true on success, false otherwise.
42 m : bool ValidateAndInferPaths(const base::FilePath& input_module,
43 m : const base::FilePath& output_module,
44 m : bool allow_overwrite,
45 m : base::FilePath* input_pdb,
46 m : base::FilePath* output_pdb);
47 :
48 : // Finalizes a block-graph, preparing it for ordering and laying out. This
49 : // performs the following operations:
50 : // - Adds metadata, if requested to.
51 : // - Update the PDB information to point to the correct PDB file.
52 : // - Finally, run the prepare headers transform. This ensures that the
53 : // header block is properly sized to receive layout information
54 : // post-ordering.
55 : // @param input_module The path to the original input module the block-graph
56 : // was built from.
57 : // @param output_pdb The path to the PDB that will refer to the transformed
58 : // PE file.
59 : // @param pdb_guid The GUID to be used in the PDB file.
60 : // @param add_metadata If true then the block-graph will be augmented with
61 : // metadata describing the transforms and Syzygy toolchain.
62 : // @param policy The policy object to be used in applying any transforms.
63 : // @param block_graph The block-graph to be finalized.
64 : // @param dos_header_block The DOS header block in the block-graph.
65 : // @returns true on success, false otherwise.
66 m : bool FinalizeBlockGraph(const base::FilePath& input_module,
67 m : const base::FilePath& output_pdb,
68 m : const GUID& pdb_guid,
69 m : bool add_metadata,
70 m : const PETransformPolicy* policy,
71 m : block_graph::BlockGraph* block_graph,
72 m : block_graph::BlockGraph::Block* dos_header_block);
73 :
74 : // Finalizes an ordered block-graph, preparing it for laying out. This simply
75 : // runs the PEOrderer which ensures that PE structures are in the appropriate
76 : // places.
77 : // @param ordered_block_graph The ordered block-graph to be finalized.
78 : // @param dos_header_block The DOS header block in the block-graph.
79 : // @returns true on success, false otherwise.
80 m : bool FinalizeOrderedBlockGraph(
81 m : block_graph::OrderedBlockGraph* ordered_block_graph,
82 m : block_graph::BlockGraph::Block* dos_header_block);
83 :
84 : // Builds an image layout for an ordered block-graph.
85 : // @param padding The minimum amount of padding to apply between blocks.
86 : // @param code_alignment The minimum alignment to enforce for code blocks.
87 : // @param ordered_block_graph The image to be laid out.
88 : // @param dos_header_block The DOS header block in the image.
89 : // @param image_layout The image-layout to be populated.
90 : // @returns true on success, false otherwise.
91 m : bool BuildImageLayout(size_t padding,
92 m : size_t code_alignment,
93 m : const block_graph::OrderedBlockGraph& ordered_block_graph,
94 m : block_graph::BlockGraph::Block* dos_header_block,
95 m : ImageLayout* image_layout);
96 :
97 : // Given the sections from an image layout calculates the source range that any
98 : // derived OMAP information must cover. This should be calculated on the
99 : // original untransformed image.
100 : // @param sections The array of sections describing the original image.
101 : // @param range The range to be populated.
102 m : void GetOmapRange(const std::vector<ImageLayout::SectionInfo>& sections,
103 m : RelativeAddressRange* range);
104 :
105 : // Given a transformed PDB file, finalizes it in preparation for writing. This
106 : // performs the following tasks:
107 : // - Sets the new GUID and clears the age count of the PDB to 1.
108 : // - Calculates OMAP information and injects it into the PDB.
109 : // - Adds/updates the Syzygy history stream which contains a record of
110 : // operations performed by the toolchain.
111 : // - If requested, serializes the block-graph to the PDB in an additional
112 : // stream.
113 : // - Finalizes the PDB header.
114 : // - Removes stream 0, the previous PDB directory stream.
115 : // @param input_module The path to the original input module.
116 : // @param output_module The path to the transformed output module.
117 : // @param input_range The input range of the original image, as calculated by
118 : // GetOmapRange.
119 : // @param image_layout The transformed image layout.
120 : // @param guid The guid of the new PDB.
121 : // @param augment_pdb If true then the serialized block-graph will be emitted to
122 : // the PDB.
123 : // @param strip_strings If true then all strings will be stripped from the
124 : // serialized block-graph, to save on space. Has no effect unless
125 : // @p augment_pdb is true.
126 : // @param compress_pdb If true then the serialized block-graph will be
127 : // compressed. Has no effect unless @p augment_pdb is true.
128 : // @param pdb_file The decomposed original PDB file to be updated.
129 : // @returns true on success, false otherwise.
130 : // @pre The transformed PE file must already have been written and finalized
131 : // prior to calling this.
132 m : bool FinalizePdbFile(const base::FilePath input_module,
133 m : const base::FilePath output_module,
134 m : const RelativeAddressRange input_range,
135 m : const ImageLayout& image_layout,
136 m : const GUID& guid,
137 m : bool augment_pdb,
138 m : bool strip_strings,
139 m : bool compress_pdb,
140 m : pdb::PdbFile* pdb_file);
141 :
142 m : } // namespace pe
143 :
144 : #endif // SYZYGY_PE_PE_RELINKER_UTIL_H_
|