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_util.h"
20 :
21 : namespace pdb {
22 :
23 E : OMAP CreateOmap(ULONG rva, ULONG rvaTo) {
24 E : OMAP omap = { rva, rvaTo };
25 E : return omap;
26 E : }
27 :
28 E : bool OmapLess(const OMAP& omap1, const OMAP& omap2) {
29 E : return omap1.rva < omap2.rva;
30 E : }
31 :
32 E : bool OmapVectorIsValid(const std::vector<OMAP>& omaps) {
33 E : for (size_t i = 1; i < omaps.size(); ++i) {
34 E : if (!OmapLess(omaps[i - 1], omaps[i]))
35 E : return false;
36 E : }
37 E : return true;
38 E : }
39 :
40 : core::RelativeAddress TranslateAddressViaOmap(const std::vector<OMAP>& omaps,
41 E : core::RelativeAddress address) {
42 E : OMAP omap_address = CreateOmap(address.value(), 0);
43 :
44 : // Find the first element that is > than omap_address.
45 : std::vector<OMAP>::const_iterator it =
46 : std::upper_bound(omaps.begin(), omaps.end(), omap_address,
47 E : OmapLess);
48 :
49 : // If we are at the first OMAP entry, the address is before any addresses
50 : // that are OMAPped. Thus, we return the same address.
51 E : if (it == omaps.begin())
52 E : return address;
53 :
54 : // Otherwise, the previous OMAP entry tells us where we lie.
55 E : --it;
56 : return core::RelativeAddress(it->rvaTo) +
57 E : (address - core::RelativeAddress(it->rva));
58 E : }
59 :
60 : bool ReadOmapsFromPdbFile(const PdbFile& pdb_file,
61 : std::vector<OMAP>* omap_to,
62 E : std::vector<OMAP>* omap_from) {
63 E : PdbStream* dbi_stream = pdb_file.GetStream(kDbiStream);
64 E : if (dbi_stream == NULL)
65 i : return false;
66 :
67 E : DbiHeader dbi_header = {};
68 E : if (!dbi_stream->Read(&dbi_header, 1))
69 i : return false;
70 :
71 E : DbiDbgHeader dbg_header = {};
72 E : if (!dbi_stream->Seek(GetDbiDbgHeaderOffset(dbi_header)))
73 i : return false;
74 E : if (!dbi_stream->Read(&dbg_header, 1))
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 E : PdbStream* omap_to_stream = pdb_file.GetStream(dbg_header.omap_to_src);
83 E : PdbStream* omap_from_stream = pdb_file.GetStream(dbg_header.omap_from_src);
84 E : if (omap_to_stream == NULL || omap_from_stream == NULL)
85 i : return false;
86 :
87 : // Read the streams if need be.
88 E : if (omap_to != NULL && !omap_to_stream->Read(omap_to))
89 i : return false;
90 E : if (omap_from != NULL && !omap_from_stream->Read(omap_from))
91 i : return false;
92 :
93 E : return true;
94 E : }
95 :
96 : bool ReadOmapsFromPdbFile(const base::FilePath& pdb_path,
97 : std::vector<OMAP>* omap_to,
98 E : std::vector<OMAP>* omap_from) {
99 E : PdbReader pdb_reader;
100 E : PdbFile pdb_file;
101 E : if (!pdb_reader.Read(pdb_path, &pdb_file))
102 E : return false;
103 E : return ReadOmapsFromPdbFile(pdb_file, omap_to, omap_from);
104 E : }
105 :
106 : } // namespace pdb
|