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