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 : // This header file provides a utility class to read a Dbi stream from a PDB and
16 : // give access to its data.
17 : //
18 : // The Dbi stream of a PDB contains different substream containing various
19 : // debug information. These different substreams are (see pdb_data.h for more
20 : // details on the sub-structures used in each substream):
21 : // - The DbiHeader;
22 : // - A set of DbiModuleInfoBase;
23 : // - A set of DbiSectionContrib;
24 : // - A set of DbiSectionMapItem;
25 : // - A set of file informations;
26 : // - The TS map (but this substream is always empty so we ignore it);
27 : // - The EC informations; and
28 : // - The DbiDbgHeader.
29 :
30 : #ifndef SYZYGY_PDB_PDB_DBI_STREAM_H_
31 : #define SYZYGY_PDB_PDB_DBI_STREAM_H_
32 :
33 : #include <map>
34 : #include <string>
35 : #include <utility>
36 : #include <vector>
37 :
38 : #include "syzygy/pdb/pdb_data.h"
39 : #include "syzygy/pdb/pdb_util.h"
40 :
41 : namespace pdb {
42 :
43 : // Forward declarations.
44 : class PdbStream;
45 :
46 : // This class represent a module info element as it is present in the module
47 : // info substream of the Dbi stream of a PDB file. It extends the
48 : // DbiModuleInfoBase structure by adding some fields with variable length.
49 : class DbiModuleInfo {
50 : public:
51 : // Default constructor.
52 E : DbiModuleInfo() {
53 E : memset(&module_info_base_, 0, sizeof(module_info_base_));
54 E : }
55 :
56 : // Copy constructor. It is required because this class is used in a STL
57 : // container.
58 : DbiModuleInfo(const DbiModuleInfo& other)
59 : : module_info_base_(other.module_info_base_),
60 : module_name_(other.module_name_),
61 E : object_name_(other.object_name_) {
62 E : }
63 :
64 : // Destructor.
65 E : ~DbiModuleInfo() {
66 E : }
67 :
68 : // Reads a module info from a PDB stream.
69 : //
70 : // @param stream The stream containing the module info. It must be positioned
71 : // at the beginning of a module info.
72 : // @returns true on success, false otherwise.
73 : bool Read(pdb::PdbStream* stream);
74 :
75 : // @name Accessors.
76 : // @{
77 E : const DbiModuleInfoBase& module_info_base() const {
78 E : return module_info_base_;
79 E : }
80 E : const std::string& module_name() const { return module_name_; }
81 : const std::string& object_name() const { return object_name_; }
82 : // @}
83 :
84 : private:
85 : DbiModuleInfoBase module_info_base_;
86 : std::string module_name_;
87 : std::string object_name_;
88 : };
89 :
90 : // This class represent the Dbi stream of a PDB. It contains some serialization
91 : // functions to be able to load the different substream.
92 : class DbiStream {
93 : public:
94 : // Typedefs used to store the content of the Dbi Stream.
95 : typedef std::vector<DbiModuleInfo> DbiModuleVector;
96 : typedef std::vector<DbiSectionContrib> DbiSectionContribVector;
97 : typedef std::vector<size_t> DbiFileInfoFileList;
98 : typedef std::vector<DbiFileInfoFileList> DbiFileInfoVector;
99 : typedef std::map<size_t, std::string> DbiFileInfoNameMap;
100 : typedef std::pair<DbiFileInfoVector, DbiFileInfoNameMap> DbiFileInfo;
101 : typedef std::map<uint16, DbiSectionMapItem> DbiSectionMap;
102 : typedef OffsetStringMap DbiEcInfoVector;
103 :
104 : // Default constructor.
105 E : DbiStream() {
106 E : }
107 :
108 : // Copy constructor.
109 : DbiStream(const DbiStream& other)
110 : : header_(other.header_),
111 : modules_(other.modules_),
112 : section_contribs_(other.section_contribs_),
113 : section_map_(other.section_map_),
114 : file_info_(other.file_info_),
115 : ec_info_vector_(other.ec_info_vector_),
116 : dbg_header_(other.dbg_header_) {
117 : }
118 :
119 : // Destructor.
120 E : ~DbiStream() {
121 E : }
122 :
123 : // @name Accessors.
124 : // @{
125 : const DbiDbgHeader& dbg_header() const { return dbg_header_; }
126 : const DbiHeader& header() const { return header_; }
127 E : const DbiModuleVector& modules() const { return modules_; }
128 : // @}
129 :
130 : // Reads the Dbi stream of a PDB.
131 : //
132 : // @param stream The stream containing the Dbi data.
133 : // @returns true on success, false otherwise.
134 : bool Read(pdb::PdbStream* stream);
135 :
136 : private:
137 : // Serialization of the Dbi header.
138 : //
139 : // @param stream The stream containing the Dbi header.
140 : // @returns true on success, false otherwise.
141 : bool ReadDbiHeaders(pdb::PdbStream* stream);
142 :
143 : // Serialization of the module info substream.
144 : //
145 : // @param stream The stream containing the module info substream.
146 : // @returns true on success, false otherwise.
147 : bool ReadDbiModuleInfo(pdb::PdbStream* stream);
148 :
149 : // Serialization of the section contribs substream.
150 : //
151 : // @param stream The stream containing the section contribs substream.
152 : // @returns true on success, false otherwise.
153 : bool ReadDbiSectionContribs(pdb::PdbStream* stream);
154 :
155 : // Serialization of the section map substream.
156 : //
157 : // @param stream The stream containing the section map substream.
158 : // @returns true on success, false otherwise.
159 : bool ReadDbiSectionMap(pdb::PdbStream* stream);
160 :
161 : // Serialization of the file info substream.
162 : //
163 : // @param stream The stream containing the file info substream.
164 : // @returns true on success, false otherwise.
165 : bool ReadDbiFileInfo(pdb::PdbStream* stream);
166 :
167 : // Serialization of the file-blocks section of the file info substream.
168 : //
169 : // @param stream The stream containing the file info substream.
170 : // @param file_blocks_table_size The size of the file-blocks table.
171 : // @param file_blocks_table_start The address of the beginning of the
172 : // file-blocks table in the stream.
173 : // @param offset_table_size The size of the offset table.
174 : // @param offset_table_start The address of the beginning of the
175 : // offset table in the stream.
176 : // @returns true on success, false otherwise.
177 : bool ReadDbiFileInfoBlocks(pdb::PdbStream* stream,
178 : uint16 file_blocks_table_size,
179 : size_t file_blocks_table_start,
180 : uint16 offset_table_size,
181 : size_t offset_table_start);
182 :
183 : // Serialization of the name table in the file info substream.
184 : //
185 : // @param stream The stream containing the file info substream.
186 : // @param name_table_start The address of the beginning of the name table in
187 : // the stream.
188 : // @param name_table_end The address of the end of the name table in the
189 : // stream.
190 : // @returns true on success, false otherwise.
191 : bool ReadDbiFileNameTable(pdb::PdbStream* stream,
192 : size_t name_table_start,
193 : size_t name_table_end);
194 :
195 : // Serialization of the EC info substream. For now we don't know for what the
196 : // EC acronym stand for. This substream is composed of a list of source file
197 : // and PDB names.
198 : //
199 : // @param stream The stream containing the EC info substream.
200 : // @returns true on success, false otherwise.
201 : bool ReadDbiECInfo(pdb::PdbStream* stream);
202 :
203 : // Header of the stream.
204 : DbiHeader header_;
205 :
206 : // All the modules we contain.
207 : DbiModuleVector modules_;
208 :
209 : // All section contributions we contain.
210 : DbiSectionContribVector section_contribs_;
211 :
212 : // Map of the sections that we contain.
213 : DbiSectionMap section_map_;
214 :
215 : // File info that we contain.
216 : DbiFileInfo file_info_;
217 :
218 : // Vector of the EC info that we contain.
219 : DbiEcInfoVector ec_info_vector_;
220 :
221 : // Debug header.
222 : DbiDbgHeader dbg_header_;
223 : };
224 :
225 : } // namespace pdb
226 :
227 : #endif // SYZYGY_PDB_PDB_DBI_STREAM_H_
|