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 : std::upper_bound(omaps.begin(), omaps.end(), omap_address,
48 E : 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 : return core::RelativeAddress(it->rvaTo) +
58 E : (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->Read(&dbi_header, 1))
70 i : return false;
71 :
72 E : DbiDbgHeader dbg_header = {};
73 E : if (!dbi_stream->Seek(GetDbiDbgHeaderOffset(dbi_header)))
74 i : return false;
75 E : if (!dbi_stream->Read(&dbg_header, 1))
76 i : return false;
77 :
78 : // We expect both the OMAP stream IDs to exist.
79 E : if (dbg_header.omap_to_src < 0 || dbg_header.omap_from_src < 0)
80 i : return false;
81 :
82 : // We expect both streams to exist.
83 E : PdbStream* omap_to_stream = pdb_file.GetStream(dbg_header.omap_to_src).get();
84 : PdbStream* omap_from_stream =
85 E : pdb_file.GetStream(dbg_header.omap_from_src).get();
86 E : if (omap_to_stream == NULL || omap_from_stream == NULL)
87 i : return false;
88 :
89 : // Read the streams if need be.
90 E : if (omap_to != NULL && !omap_to_stream->Read(omap_to))
91 i : return false;
92 E : if (omap_from != NULL && !omap_from_stream->Read(omap_from))
93 i : return false;
94 :
95 E : return true;
96 E : }
97 :
98 : bool ReadOmapsFromPdbFile(const base::FilePath& pdb_path,
99 : std::vector<OMAP>* omap_to,
100 E : std::vector<OMAP>* omap_from) {
101 E : PdbReader pdb_reader;
102 E : PdbFile pdb_file;
103 E : if (!pdb_reader.Read(pdb_path, &pdb_file))
104 E : return false;
105 E : return ReadOmapsFromPdbFile(pdb_file, omap_to, omap_from);
106 E : }
107 :
108 : } // namespace pdb
|