1 : // Copyright 2012 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 : #ifndef SYZYGY_PE_PE_UTILS_H_
16 : #define SYZYGY_PE_PE_UTILS_H_
17 :
18 : #include <windows.h>
19 : #include <winnt.h>
20 :
21 : #include "syzygy/block_graph/block_graph.h"
22 :
23 m : namespace pe {
24 :
25 : // Typical section names.
26 m : extern const char kCodeSectionName[];
27 m : extern const char kReadOnlyDataSectionName[];
28 m : extern const char kReadWriteDataSectionName[];
29 m : extern const char kRelocSectionName[];
30 m : extern const char kResourceSectionName[];
31 m : extern const char kTlsSectionName[];
32 :
33 : // Typical section characteristics.
34 m : extern const DWORD kCodeCharacteristics;
35 m : extern const DWORD kReadOnlyDataCharacteristics;
36 m : extern const DWORD kReadWriteDataCharacteristics;
37 m : extern const DWORD kRelocCharacteristics;
38 :
39 : // Validates @p dos_header_block for the size, magic constants and other
40 : // properties of a valid DOS header.
41 : // @returns true iff @p dos_header_block has all the correct properties
42 : // of a DOS header.
43 m : bool IsValidDosHeaderBlock(
44 m : const block_graph::BlockGraph::Block* dos_header_block);
45 :
46 : // Validates @p nt_headers_block for the the size, magic constants and
47 : // other properties of valid NT headers.
48 : // @returns true iff block has correct size and signature for a DOS
49 : // header block.
50 m : bool IsValidNtHeadersBlock(
51 m : const block_graph::BlockGraph::Block* nt_headers_block);
52 :
53 : // Retrieves and validates the NT headers block from a valid DOS headers block.
54 : // @returns the NT headers block, iff it can be retrieved from the DOS headers
55 : // block, and if the NT headers block has valid signatures.
56 m : const block_graph::BlockGraph::Block* GetNtHeadersBlockFromDosHeaderBlock(
57 m : const block_graph::BlockGraph::Block* dos_header_block);
58 m : block_graph::BlockGraph::Block* GetNtHeadersBlockFromDosHeaderBlock(
59 m : block_graph::BlockGraph::Block* dos_header_block);
60 :
61 : // Updates the provided DOS header block in preparation for writing a module
62 : // from a BlockGraph. Trims any superfluous data and inserts a new DOS stub.
63 : // After this has been applied IsValidDosHeaderBlock will succeed.
64 : // @param dos_header_block the DOS header block to update.
65 : // @returns true on success, false otherwise.
66 m : bool UpdateDosHeader(block_graph::BlockGraph::Block* dos_header_block);
67 :
68 m : typedef std::pair<block_graph::BlockGraph::Block*,
69 m : block_graph::BlockGraph::Offset> EntryPoint;
70 m : typedef std::set<EntryPoint> EntryPointSet;
71 :
72 : // Retrieves the image entry point into @p entry_points IFF the image is an
73 : // EXE. If the image is not an EXE then this is a NOP.
74 : // @param dos_header_block the DOS header block of the image.
75 : // @param entry_points the entry-point will be inserted into this set if the
76 : // image in question is an executable.
77 : // @returns true on success, false otherwise. It is not considered a failure
78 : // if @p entry_points is left unchanged because @p dos_header_block
79 : // indicates that the image is not an executable.
80 : // @note The returned @p entry_point will have a call-signature taking no
81 : // arguments.
82 m : bool GetExeEntryPoint(block_graph::BlockGraph::Block* dos_header_block,
83 m : EntryPoint* entry_point);
84 :
85 : // Retrieves the image entry point into @p entry_points IFF the image is a
86 : // DLL. If the image is not a DLL, or if the DLL has no entry point, then this
87 : // is a NOP.
88 : // @param dos_header_block the DOS header block of the image.
89 : // @param entry_points the entry-point will be inserted into this set if the
90 : // image in question is a DLL. Note that the entry-point for a DLL is
91 : // optional; if the DLL has no entry point, the Block* of the returned
92 : // EntryPoint structure will be NULL.
93 : // @returns true on success, false otherwise. It is not considered a failure
94 : // if @p entry_points is left unchanged because @p dos_header_block
95 : // indicates that the image is not a DLL.
96 : // @note The returned @p entry_point, if any, will have a call-signature
97 : // matching that of DllMain.
98 m : bool GetDllEntryPoint(block_graph::BlockGraph::Block* dos_header_block,
99 m : EntryPoint* entry_point);
100 :
101 : // Retrieves the TLS initializer entry-points into @p entry_points.
102 : // @param dos_header_block the DOS header block of the image.
103 : // @param entry_points the entry-point will be inserted into this set if the
104 : // image in question is a DLL. If the set already contains elements it will
105 : // be added to.
106 : // @returns true on success, false otherwise.
107 : // @note The returned @p entry_points, if any, will have a call-signature
108 : // matching that of DllMain.
109 : // TODO(rogerm): We may want to change this to output to an EntryPointVector
110 : // instead of to a set. This would be more consistent with the actual
111 : // representation of the TLS initializers. That said, our actual usage of
112 : // the returned entry-points would require us to eliminate duplicates after
113 : // the fact. Left as a set for now, under suspicion of YAGNI.
114 m : bool GetTlsInitializers(block_graph::BlockGraph::Block* dos_header_block,
115 m : EntryPointSet* entry_points);
116 :
117 : // Check if an image contains an import entry.
118 : // @param The image's header-block.
119 : // @param dll_name The name of the DLL.
120 : // @param contains_dependence Boolean to indicate if the image contains the
121 : // import entry.
122 : // @returns true in case of success, false otherwise.
123 m : bool HasImportEntry(block_graph::BlockGraph::Block* header_block,
124 m : const base::StringPiece& dll_name,
125 m : bool* has_import_entry);
126 m : } // namespace pe
127 :
128 : #endif // SYZYGY_PE_PE_UTILS_H_
|