@@ -48,16 +48,16 @@ struct PDBDbiStream::HeaderInfo {
48
48
little32_t VersionSignature;
49
49
ulittle32_t VersionHeader;
50
50
ulittle32_t Age; // Should match PDBInfoStream.
51
- ulittle16_t GSSyms;
52
- ulittle16_t BuildNumber; // See DbiBuildNo structure.
53
- ulittle16_t PSSyms;
51
+ ulittle16_t GSSyms; // Number of global symbols
52
+ ulittle16_t BuildNumber; // See DbiBuildNo structure.
53
+ ulittle16_t PSSyms; // Number of public symbols
54
54
ulittle16_t PdbDllVersion; // version of mspdbNNN.dll
55
55
ulittle16_t SymRecords; // Number of symbols
56
56
ulittle16_t PdbDllRbld; // rbld number of mspdbNNN.dll
57
57
little32_t ModiSubstreamSize; // Size of module info stream
58
58
little32_t SecContrSubstreamSize; // Size of sec. contribution stream
59
- little32_t SectionMapSize;
60
- little32_t FileInfoSize;
59
+ little32_t SectionMapSize; // Size of sec. map substream
60
+ little32_t FileInfoSize; // Size of file info substream
61
61
little32_t TypeServerSize; // Size of type server map
62
62
ulittle32_t MFCTypeServerIndex; // Index of MFC Type Server
63
63
little32_t OptionalDbgHdrSize; // Size of DbgHeader info
@@ -101,12 +101,42 @@ std::error_code PDBDbiStream::reload() {
101
101
Header->OptionalDbgHdrSize + Header->ECSubstreamSize )
102
102
return std::make_error_code (std::errc::illegal_byte_sequence);
103
103
104
+ // Only certain substreams are guaranteed to be aligned. Validate
105
+ // them here.
104
106
if (Header->ModiSubstreamSize % sizeof (uint32_t ) != 0 )
105
107
return std::make_error_code (std::errc::illegal_byte_sequence);
108
+ if (Header->SecContrSubstreamSize % sizeof (uint32_t ) != 0 )
109
+ return std::make_error_code (std::errc::illegal_byte_sequence);
110
+ if (Header->SectionMapSize % sizeof (uint32_t ) != 0 )
111
+ return std::make_error_code (std::errc::illegal_byte_sequence);
112
+ if (Header->FileInfoSize % sizeof (uint32_t ) != 0 )
113
+ return std::make_error_code (std::errc::illegal_byte_sequence);
114
+ if (Header->TypeServerSize % sizeof (uint32_t ) != 0 )
115
+ return std::make_error_code (std::errc::illegal_byte_sequence);
116
+
117
+ std::error_code EC;
118
+ if (EC = readSubstream (ModInfoSubstream, Header->ModiSubstreamSize ))
119
+ return EC;
120
+
121
+ // Since each ModInfo in the stream is a variable length, we have to iterate
122
+ // them to know how many there actually are.
123
+ auto Range = llvm::make_range (ModInfoIterator (&ModInfoSubstream.front ()),
124
+ ModInfoIterator (&ModInfoSubstream.back () + 1 ));
125
+ for (auto Info : Range)
126
+ ModuleInfos.push_back (ModuleInfoEx (Info));
106
127
107
- ModInfoSubstream.resize (Header->ModiSubstreamSize );
108
- if (auto EC =
109
- Stream.readBytes (&ModInfoSubstream[0 ], Header->ModiSubstreamSize ))
128
+ if (EC = readSubstream (SecContrSubstream, Header->SecContrSubstreamSize ))
129
+ return EC;
130
+ if (EC = readSubstream (SecMapSubstream, Header->SectionMapSize ))
131
+ return EC;
132
+ if (EC = readSubstream (FileInfoSubstream, Header->FileInfoSize ))
133
+ return EC;
134
+ if (EC = readSubstream (TypeServerMapSubstream, Header->TypeServerSize ))
135
+ return EC;
136
+ if (EC = readSubstream (ECSubstream, Header->ECSubstreamSize ))
137
+ return EC;
138
+
139
+ if (EC = initializeFileInfo ())
110
140
return EC;
111
141
112
142
return std::error_code ();
@@ -150,7 +180,90 @@ PDB_Machine PDBDbiStream::getMachineType() const {
150
180
return static_cast <PDB_Machine>(Machine);
151
181
}
152
182
153
- llvm::iterator_range<ModInfoIterator> PDBDbiStream::modules () const {
154
- return llvm::make_range (ModInfoIterator (&ModInfoSubstream.front ()),
155
- ModInfoIterator (&ModInfoSubstream.back () + 1 ));
183
+ ArrayRef<ModuleInfoEx> PDBDbiStream::modules () const { return ModuleInfos; }
184
+
185
+ std::error_code PDBDbiStream::readSubstream (std::vector<uint8_t > &Bytes, uint32_t Size) {
186
+ Bytes.clear ();
187
+ if (Size == 0 )
188
+ return std::error_code ();
189
+
190
+ Bytes.resize (Size);
191
+ return Stream.readBytes (&Bytes[0 ], Size);
192
+ }
193
+
194
+ std::error_code PDBDbiStream::initializeFileInfo () {
195
+ struct FileInfoSubstreamHeader {
196
+ ulittle16_t NumModules; // Total # of modules, should match number of
197
+ // records in the ModuleInfo substream.
198
+ ulittle16_t NumSourceFiles; // Total # of source files. This value is not
199
+ // accurate because PDB actually supports more
200
+ // than 64k source files, so we ignore it and
201
+ // compute the value from other stream fields.
202
+ };
203
+
204
+ // The layout of the FileInfoSubstream is like this:
205
+ // struct {
206
+ // ulittle16_t NumModules;
207
+ // ulittle16_t NumSourceFiles;
208
+ // ulittle16_t ModIndices[NumModules];
209
+ // ulittle16_t ModFileCounts[NumModules];
210
+ // ulittle32_t FileNameOffsets[NumSourceFiles];
211
+ // char Names[][NumSourceFiles];
212
+ // };
213
+ // with the caveat that `NumSourceFiles` cannot be trusted, so
214
+ // it is computed by summing `ModFileCounts`.
215
+ //
216
+ const uint8_t *Buf = &FileInfoSubstream[0 ];
217
+ auto FI = reinterpret_cast <const FileInfoSubstreamHeader *>(Buf);
218
+ Buf += sizeof (FileInfoSubstreamHeader);
219
+ // The number of modules in the stream should be the same as reported by
220
+ // the FileInfoSubstreamHeader.
221
+ if (FI->NumModules != ModuleInfos.size ())
222
+ return std::make_error_code (std::errc::illegal_byte_sequence);
223
+
224
+ // First is an array of `NumModules` module indices. This is not used for the
225
+ // same reason that `NumSourceFiles` is not used. It's an array of uint16's,
226
+ // but it's possible there are more than 64k source files, which would imply
227
+ // more than 64k modules (e.g. object files) as well. So we ignore this
228
+ // field.
229
+ llvm::ArrayRef<ulittle16_t > ModIndexArray (
230
+ reinterpret_cast <const ulittle16_t *>(Buf), ModuleInfos.size ());
231
+
232
+ llvm::ArrayRef<ulittle16_t > ModFileCountArray (ModIndexArray.end (),
233
+ ModuleInfos.size ());
234
+
235
+ // Compute the real number of source files.
236
+ uint32_t NumSourceFiles = 0 ;
237
+ for (auto Count : ModFileCountArray)
238
+ NumSourceFiles += Count;
239
+
240
+ // This is the array that in the reference implementation corresponds to
241
+ // `ModInfo::FileLayout::FileNameOffs`, which is commented there as being a
242
+ // pointer. Due to the mentioned problems of pointers causing difficulty
243
+ // when reading from the file on 64-bit systems, we continue to ignore that
244
+ // field in `ModInfo`, and instead build a vector of StringRefs and stores
245
+ // them in `ModuleInfoEx`. The value written to and read from the file is
246
+ // not used anyway, it is only there as a way to store the offsets for the
247
+ // purposes of later accessing the names at runtime.
248
+ llvm::ArrayRef<little32_t > FileNameOffsets (
249
+ reinterpret_cast <const little32_t *>(ModFileCountArray.end ()),
250
+ NumSourceFiles);
251
+
252
+ const char *Names = reinterpret_cast <const char *>(FileNameOffsets.end ());
253
+
254
+ // We go through each ModuleInfo, determine the number N of source files for
255
+ // that module, and then get the next N offsets from the Offsets array, using
256
+ // them to get the corresponding N names from the Names buffer and associating
257
+ // each one with the corresponding module.
258
+ uint32_t NextFileIndex = 0 ;
259
+ for (size_t I = 0 ; I < ModuleInfos.size (); ++I) {
260
+ uint32_t NumFiles = ModFileCountArray[I];
261
+ ModuleInfos[I].SourceFiles .resize (NumFiles);
262
+ for (size_t J = 0 ; J < NumFiles; ++J, ++NextFileIndex) {
263
+ uint32_t FileIndex = FileNameOffsets[NextFileIndex];
264
+ ModuleInfos[I].SourceFiles [J] = StringRef (Names + FileIndex);
265
+ }
266
+ }
267
+
268
+ return std::error_code ();
156
269
}
0 commit comments