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 : #include "syzygy/pdb/omap.h"
16 :
17 : #include <algorithm>
18 :
19 : #include "syzygy/pdb/pdb_file.h"
20 : #include "syzygy/pdb/pdb_util.h"
21 :
22 : namespace pdb {
23 :
24 E : OMAP CreateOmap(ULONG rva, ULONG rvaTo) {
25 E : OMAP omap = { rva, rvaTo };
26 E : return omap;
27 E : }
28 :
29 E : bool OmapLess(const OMAP& omap1, const OMAP& omap2) {
30 E : return omap1.rva < omap2.rva;
31 E : }
32 :
33 E : bool OmapVectorIsValid(const std::vector<OMAP>& omaps) {
34 E : for (size_t i = 1; i < omaps.size(); ++i) {
35 E : if (!OmapLess(omaps[i - 1], omaps[i]))
36 E : return false;
37 E : }
38 E : return true;
39 E : }
40 :
41 : core::RelativeAddress TranslateAddressViaOmap(const std::vector<OMAP>& omaps,
42 E : core::RelativeAddress address) {
43 E : OMAP omap_address = CreateOmap(address.value(), 0);
44 :
45 : // Find the first element that is > than omap_address.
46 : std::vector<OMAP>::const_iterator it =
47 E : std::upper_bound(omaps.begin(), omaps.end(), omap_address,
48 : OmapLess);
49 :
50 : // If we are at the first OMAP entry, the address is before any addresses
51 : // that are OMAPped. Thus, we return the same address.
52 E : if (it == omaps.begin())
53 E : return address;
54 :
55 : // Otherwise, the previous OMAP entry tells us where we lie.
56 E : --it;
57 E : return core::RelativeAddress(it->rvaTo) +
58 : (address - core::RelativeAddress(it->rva));
59 E : }
60 :
61 : bool ReadOmapsFromPdbFile(const PdbFile& pdb_file,
62 : std::vector<OMAP>* omap_to,
63 E : std::vector<OMAP>* omap_from) {
64 E : PdbStream* dbi_stream = pdb_file.GetStream(kDbiStream).get();
65 E : if (dbi_stream == NULL)
66 i : return false;
67 :
68 E : DbiHeader dbi_header = {};
69 E : if (!dbi_stream->ReadBytesAt(0, sizeof(dbi_header), &dbi_header))
70 i : return false;
71 :
72 E : DbiDbgHeader dbg_header = {};
73 E : size_t offset = GetDbiDbgHeaderOffset(dbi_header);
74 E : if (!dbi_stream->ReadBytesAt(offset, sizeof(dbg_header), &dbg_header))
75 i : return false;
76 :
77 : // We expect both the OMAP stream IDs to exist.
78 E : if (dbg_header.omap_to_src < 0 || dbg_header.omap_from_src < 0)
79 i : return false;
80 :
81 : // We expect both streams to exist.
82 : scoped_refptr<PdbStream> omap_to_stream =
83 E : pdb_file.GetStream(dbg_header.omap_to_src);
84 : scoped_refptr<PdbStream> omap_from_stream =
85 E : pdb_file.GetStream(dbg_header.omap_from_src);
86 E : if (omap_to_stream == nullptr || omap_from_stream == nullptr)
87 i : return false;
88 :
89 E : DCHECK(omap_to_stream != nullptr && omap_from_stream != nullptr);
90 : // Read the streams if need be.
91 E : size_t num_to = omap_to_stream->length() / sizeof(OMAP);
92 E : if (omap_to != nullptr) {
93 E : omap_to->resize(num_to);
94 E : if (num_to &&
95 : !omap_to_stream->ReadBytesAt(0, num_to * sizeof(OMAP),
96 : &omap_to->at(0))) {
97 i : omap_to->clear();
98 i : return false;
99 : }
100 : }
101 :
102 E : if (omap_from != nullptr) {
103 E : size_t num_from = omap_from_stream->length() / sizeof(OMAP);
104 E : omap_from->resize(num_from);
105 E : if (num_from &&
106 : !omap_from_stream->ReadBytesAt(0, num_from * sizeof(OMAP),
107 : &omap_from->at(0))) {
108 i : omap_from->clear();
109 i : return false;
110 : }
111 : }
112 :
113 E : return true;
114 E : }
115 :
116 : bool ReadOmapsFromPdbFile(const base::FilePath& pdb_path,
117 : std::vector<OMAP>* omap_to,
118 E : std::vector<OMAP>* omap_from) {
119 E : PdbReader pdb_reader;
120 E : PdbFile pdb_file;
121 E : if (!pdb_reader.Read(pdb_path, &pdb_file))
122 E : return false;
123 E : return ReadOmapsFromPdbFile(pdb_file, omap_to, omap_from);
124 E : }
125 :
126 : } // namespace pdb
|