1 : // Copyright 2014 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 : // Utilities that are specific to dealing with COFF files in block-graph
16 : // representation.
17 :
18 : #ifndef SYZYGY_PE_COFF_UTILS_H_
19 : #define SYZYGY_PE_COFF_UTILS_H_
20 :
21 : #include <windows.h>
22 : #include <winnt.h>
23 :
24 : #include "base/callback.h"
25 : #include "syzygy/block_graph/block_graph.h"
26 :
27 m : namespace pe {
28 :
29 : // Retrieve the blocks containing the headers, symbol and strings tables
30 : // from the block graph. Each of @p headers_block, @p symbols_block and
31 : // @p strings_block may be NULL if the corresponding block needs not be
32 : // retrieved.
33 : //
34 : // @param block_graph the graph to extract blocks from.
35 : // @param headers_block where to store the headers block.
36 : // @param symbols_block where to store the symbol table block.
37 : // @param strings_block where to store the string table block.
38 : // @returns true if all three blocks are found, false otherwise.
39 m : bool FindCoffSpecialBlocks(block_graph::BlockGraph* block_graph,
40 m : block_graph::BlockGraph::Block** headers_block,
41 m : block_graph::BlockGraph::Block** symbols_block,
42 m : block_graph::BlockGraph::Block** strings_block);
43 :
44 : // Gets the name of a symbol in the given block and offset.
45 : // @param symbols_block block containing symbols.
46 : // @param strings_block block containing strings.
47 : // @param symbol_offset The offset of the symbol in question.
48 : // @param name receives the name of the symbol.
49 : // @returns true on success, false otherwise.
50 m : bool GetCoffSymbolName(const block_graph::BlockGraph::Block* symbols_block,
51 m : const block_graph::BlockGraph::Block* strings_block,
52 m : block_graph::BlockGraph::Offset symbol_offset,
53 m : base::StringPiece* name);
54 :
55 : // Visitor callback for symbol iteration.
56 : // |symbols_block| is the block containing the symbols.
57 : // |strings_block| is the block containing the strings.
58 : // |symbol_offset| is the offset of the symbol.
59 : // The callback returns true to indicate success and continue the iteration,
60 : // and false to indicate an error and abort the iteraton.
61 m : typedef base::Callback<
62 m : bool(block_graph::BlockGraph::Block* /* symbols_block */,
63 m : block_graph::BlockGraph::Block* /* strings_block */,
64 m : block_graph::BlockGraph::Offset /* symbol_offset */)>
65 m : VisitCoffSymbolCallback;
66 :
67 : // @{
68 : // Iterates over the symbols in a COFF image.
69 : // @param callback is the callback to invoke for each symbol.
70 : // @param symbols_block is the block containing the symbols.
71 : // @param strings_block is the block containing the strings.
72 : // @param block_graph is the block-graph containing a decomposed COFF image.
73 : // @returns true on success, false otherwise.
74 m : bool VisitCoffSymbols(const VisitCoffSymbolCallback& callback,
75 m : block_graph::BlockGraph::Block* symbols_block,
76 m : block_graph::BlockGraph::Block* strings_block);
77 m : bool VisitCoffSymbols(const VisitCoffSymbolCallback& callback,
78 m : block_graph::BlockGraph* block_graph);
79 : // @}
80 :
81 m : typedef std::set<block_graph::BlockGraph::Offset> CoffSymbolOffsets;
82 :
83 : // @{
84 : // Searches for a COFF symbol by name and returns its offset if found.
85 : // @param symbol_name is the name of the symbol to find.
86 : // @param symbols_block is the block containing the symbols.
87 : // @param strings_block is the block containing the strings.
88 : // @param block_graph is the block-graph containing a decomposed COFF image.
89 : // @param symbol_offsets will be populated with the offsets of the corresponding
90 : // symbols. If no symbol is found this will be empty.
91 : // @returns true on completion, false on error.
92 m : bool FindCoffSymbol(const base::StringPiece& symbol_name,
93 m : block_graph::BlockGraph::Block* symbols_block,
94 m : block_graph::BlockGraph::Block* strings_block,
95 m : CoffSymbolOffsets* symbol_offsets);
96 m : bool FindCoffSymbol(const base::StringPiece& symbol_name,
97 m : block_graph::BlockGraph* block_graph,
98 m : CoffSymbolOffsets* symbol_offsets);
99 : // @}
100 :
101 : // Used for mapping COFF symbols from their name to their offset(s) in the
102 : // symbol block.
103 m : typedef std::map<std::string, CoffSymbolOffsets> CoffSymbolNameOffsetMap;
104 :
105 : // @{
106 : // Builds a map of COFF symbols by name, mapped to their offset in the symbols
107 : // block. Special COFF symbols that are multiply defined map to
108 : // kDuplicateCoffSymbol.
109 : // @param symbols_block is the block containing the symbols.
110 : // @param strings_block is the block containing the strings.
111 : // @param block_graph is the block-graph containing a decomposed COFF image.
112 : // @param map the map to be populated.
113 : // @return true on success, false otherwise.
114 m : bool BuildCoffSymbolNameOffsetMap(
115 m : block_graph::BlockGraph::Block* symbols_block,
116 m : block_graph::BlockGraph::Block* strings_block,
117 m : CoffSymbolNameOffsetMap* map);
118 m : bool BuildCoffSymbolNameOffsetMap(
119 m : block_graph::BlockGraph* block_graph,
120 m : CoffSymbolNameOffsetMap* map);
121 : // @}
122 :
123 m : } // namespace pe
124 :
125 : #endif // SYZYGY_PE_COFF_UTILS_H_
|