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 : namespace refinery {
23 :
24 E : SymbolProvider::SymbolProvider() {
25 E : }
26 :
27 E : SymbolProvider::~SymbolProvider() {
28 E : }
29 :
30 : bool SymbolProvider::FindOrCreateTypeRepository(
31 : const pe::PEFile::Signature& signature,
32 E : scoped_refptr<TypeRepository>* type_repo) {
33 E : DCHECK(type_repo);
34 E : *type_repo = nullptr;
35 :
36 E : base::string16 cache_key;
37 E : GetCacheKey(signature, &cache_key);
38 :
39 : SimpleCache<TypeRepository>::LoadingCallback load_cb = base::Bind(
40 E : &SymbolProvider::CreateTypeRepository, base::Unretained(this), signature);
41 :
42 E : type_repos_.GetOrLoad(cache_key, load_cb, type_repo);
43 E : return type_repo->get() != nullptr;
44 E : }
45 :
46 : bool SymbolProvider::FindOrCreateTypeNameIndex(
47 : const pe::PEFile::Signature& signature,
48 E : scoped_refptr<TypeNameIndex>* typename_index) {
49 E : DCHECK(typename_index);
50 E : *typename_index = nullptr;
51 :
52 E : base::string16 cache_key;
53 E : GetCacheKey(signature, &cache_key);
54 :
55 : SimpleCache<TypeNameIndex>::LoadingCallback load_cb = base::Bind(
56 E : &SymbolProvider::CreateTypeNameIndex, base::Unretained(this), signature);
57 :
58 E : typename_indices_.GetOrLoad(cache_key, load_cb, typename_index);
59 E : return typename_index->get() != nullptr;
60 E : }
61 :
62 : void SymbolProvider::GetCacheKey(const pe::PEFile::Signature& signature,
63 E : base::string16* cache_key) {
64 E : DCHECK(cache_key);
65 : // Note that the cache key does not contain the module's base address.
66 : base::SStringPrintf(cache_key, L"%ls:%d:%d:%d",
67 : base::FilePath(signature.path).BaseName().value().c_str(),
68 : signature.module_size, signature.module_checksum,
69 E : signature.module_time_date_stamp);
70 E : }
71 :
72 : bool SymbolProvider::CreateTypeRepository(
73 : const pe::PEFile::Signature& signature,
74 E : scoped_refptr<TypeRepository>* type_repo) {
75 E : DCHECK(type_repo);
76 E : *type_repo = nullptr;
77 :
78 E : base::FilePath pdb_path;
79 E : if (!GetPdbPath(signature, &pdb_path))
80 i : return false;
81 :
82 E : scoped_refptr<TypeRepository> repository = new TypeRepository();
83 E : PdbCrawler crawler;
84 : if (!crawler.InitializeForFile(pdb_path) ||
85 E : !crawler.GetTypes(repository.get())) {
86 i : return false;
87 : }
88 :
89 E : *type_repo = repository;
90 E : return true;
91 E : }
92 :
93 : bool SymbolProvider::CreateTypeNameIndex(const pe::PEFile::Signature& signature,
94 E : scoped_refptr<TypeNameIndex>* index) {
95 E : DCHECK(index);
96 :
97 E : scoped_refptr<TypeRepository> repository;
98 E : if (!FindOrCreateTypeRepository(signature, &repository))
99 i : return false;
100 :
101 E : *index = new TypeNameIndex(repository);
102 E : return true;
103 E : }
104 :
105 : } // namespace refinery
|