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 : #include "syzygy/pe/transforms/coff_convert_legacy_code_references_transform.h"
16 :
17 : namespace pe {
18 : namespace transforms {
19 : namespace {
20 :
21 : using block_graph::BlockGraph;
22 :
23 : // Convert all non-relocation references to equivalent relocation references
24 : // in @p block.
25 : // @param block the block whose references are to be converted.
26 E : void ConvertReferences(BlockGraph::Block* block) {
27 E : DCHECK_NE(reinterpret_cast<BlockGraph::Block*>(NULL), block);
28 :
29 : BlockGraph::Block::ReferenceMap::const_iterator it =
30 E : block->references().begin();
31 E : for (; it != block->references().end(); ++it) {
32 E : if ((it->second.type() & BlockGraph::RELOC_REF_BIT) != 0)
33 E : continue;
34 : BlockGraph::ReferenceType new_type =
35 : static_cast<BlockGraph::ReferenceType>(
36 E : it->second.type() | BlockGraph::RELOC_REF_BIT);
37 : BlockGraph::Reference ref(new_type,
38 : it->second.size(),
39 : it->second.referenced(),
40 : it->second.offset(),
41 E : it->second.base());
42 : // We expect this to return false, as the reference already exists.
43 E : CHECK(!block->SetReference(it->first, ref));
44 E : }
45 : return;
46 E : }
47 :
48 : } // namespace
49 :
50 : const char CoffConvertLegacyCodeReferencesTransform::kTransformName[] =
51 : "CoffConvertLegacyCodeReferencesTransform";
52 :
53 : bool CoffConvertLegacyCodeReferencesTransform::TransformBlockGraph(
54 : const TransformPolicyInterface* policy,
55 : BlockGraph* block_graph,
56 E : BlockGraph::Block* /* headers_block */) {
57 E : DCHECK_NE(reinterpret_cast<TransformPolicyInterface*>(NULL), policy);
58 E : DCHECK_NE(reinterpret_cast<BlockGraph*>(NULL), block_graph);
59 E : DCHECK_EQ(BlockGraph::COFF_IMAGE, block_graph->image_format());
60 :
61 E : BlockGraph::BlockMap::iterator it = block_graph->blocks_mutable().begin();
62 E : for (; it != block_graph->blocks_mutable().end(); ++it) {
63 E : if (it->second.type() == BlockGraph::CODE_BLOCK)
64 E : ConvertReferences(&it->second);
65 E : }
66 E : return true;
67 E : }
68 :
69 : } // namespace transforms
70 : } // namespace pe
|