1 : // Copyright 2015 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/refinery/symbols/symbol_provider.h"
16 :
17 : #include "base/bind.h"
18 : #include "base/strings/stringprintf.h"
19 : #include "syzygy/refinery/symbols/symbol_provider_util.h"
20 : #include "syzygy/refinery/types/pdb_crawler.h"
21 :
22 m : namespace refinery {
23 :
24 m : SymbolProvider::SymbolProvider() {
25 m : }
26 :
27 m : SymbolProvider::~SymbolProvider() {
28 m : }
29 :
30 m : bool SymbolProvider::FindOrCreateTypeRepository(
31 m : const pe::PEFile::Signature& signature,
32 m : scoped_refptr<TypeRepository>* type_repo) {
33 m : DCHECK(type_repo);
34 m : *type_repo = nullptr;
35 :
36 m : base::string16 cache_key;
37 m : GetCacheKey(signature, &cache_key);
38 :
39 m : SimpleCache<TypeRepository>::LoadingCallback load_cb = base::Bind(
40 m : &SymbolProvider::CreateTypeRepository, base::Unretained(this), signature);
41 :
42 m : type_repos_.GetOrLoad(cache_key, load_cb, type_repo);
43 m : return type_repo->get() != nullptr;
44 m : }
45 :
46 m : bool SymbolProvider::FindOrCreateTypeNameIndex(
47 m : const pe::PEFile::Signature& signature,
48 m : scoped_refptr<TypeNameIndex>* typename_index) {
49 m : DCHECK(typename_index);
50 m : *typename_index = nullptr;
51 :
52 m : base::string16 cache_key;
53 m : GetCacheKey(signature, &cache_key);
54 :
55 m : SimpleCache<TypeNameIndex>::LoadingCallback load_cb = base::Bind(
56 m : &SymbolProvider::CreateTypeNameIndex, base::Unretained(this), signature);
57 :
58 m : typename_indices_.GetOrLoad(cache_key, load_cb, typename_index);
59 m : return typename_index->get() != nullptr;
60 m : }
61 :
62 m : bool SymbolProvider::GetVFTableRVAs(
63 m : const pe::PEFile::Signature& signature,
64 m : base::hash_set<RelativeAddress>* vftable_rvas) {
65 m : DCHECK(vftable_rvas);
66 m : vftable_rvas->clear();
67 :
68 m : base::FilePath pdb_path;
69 m : if (!GetPdbPath(signature, &pdb_path))
70 m : return false;
71 :
72 m : PdbCrawler crawler;
73 m : if (!crawler.InitializeForFile(pdb_path))
74 m : return false;
75 :
76 m : return crawler.GetVFTableRVAs(vftable_rvas);
77 m : }
78 :
79 m : void SymbolProvider::GetCacheKey(const pe::PEFile::Signature& signature,
80 m : base::string16* cache_key) {
81 m : DCHECK(cache_key);
82 : // Note that the cache key does not contain the module's base address.
83 m : base::SStringPrintf(cache_key, L"%ls:%d:%d:%d",
84 m : base::FilePath(signature.path).BaseName().value().c_str(),
85 m : signature.module_size, signature.module_checksum,
86 m : signature.module_time_date_stamp);
87 m : }
88 :
89 m : bool SymbolProvider::CreateTypeRepository(
90 m : const pe::PEFile::Signature& signature,
91 m : scoped_refptr<TypeRepository>* type_repo) {
92 m : DCHECK(type_repo);
93 m : *type_repo = nullptr;
94 :
95 m : base::FilePath pdb_path;
96 m : if (!GetPdbPath(signature, &pdb_path))
97 m : return false;
98 :
99 m : scoped_refptr<TypeRepository> repository = new TypeRepository();
100 m : PdbCrawler crawler;
101 m : if (!crawler.InitializeForFile(pdb_path) ||
102 m : !crawler.GetTypes(repository.get())) {
103 m : return false;
104 m : }
105 :
106 m : *type_repo = repository;
107 m : return true;
108 m : }
109 :
110 m : bool SymbolProvider::CreateTypeNameIndex(const pe::PEFile::Signature& signature,
111 m : scoped_refptr<TypeNameIndex>* index) {
112 m : DCHECK(index);
113 :
114 m : scoped_refptr<TypeRepository> repository;
115 m : if (!FindOrCreateTypeRepository(signature, &repository))
116 m : return false;
117 :
118 m : *index = new TypeNameIndex(repository);
119 m : return true;
120 m : }
121 :
122 m : } // namespace refinery
|