LLVM 24.0.0git
TypeIndexDiscovery.cpp
Go to the documentation of this file.
1//===- TypeIndexDiscovery.cpp -----------------------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
10#include "llvm/ADT/ArrayRef.h"
12#include "llvm/Support/Endian.h"
13
14using namespace llvm;
15using namespace llvm::codeview;
16
17static inline MethodKind getMethodKind(uint16_t Attrs) {
19 Attrs >>= 2;
20 return MethodKind(Attrs);
21}
22
23static inline bool isIntroVirtual(uint16_t Attrs) {
24 MethodKind MK = getMethodKind(Attrs);
25 return MK == MethodKind::IntroducingVirtual ||
27}
28
29static inline PointerMode getPointerMode(uint32_t Attrs) {
30 return static_cast<PointerMode>((Attrs >> PointerRecord::PointerModeShift) &
32}
33
39
42 if (N < LF_NUMERIC)
43 return 2;
44
45 if (N > LF_DATE)
46 return 0;
47
48 constexpr uint32_t Sizes[] = {
49 1, // LF_CHAR
50 2, // LF_SHORT
51 2, // LF_USHORT
52 4, // LF_LONG
53 4, // LF_ULONG
54 4, // LF_REAL32
55 8, // LF_REAL64
56 10, // LF_REAL80
57 16, // LF_REAL128
58 8, // LF_QUADWORD
59 8, // LF_UQUADWORD
60 6, // LF_REAL48
61 8, // LF_COMPLEX32
62 16, // LF_COMPLEX64
63 20, // LF_COMPLEX80
64 32, // LF_COMPLEX128
65 0, // LF_VARSTRING (variable length)
66 0, // 0x8011 (Unused)
67 0, // 0x8012 (Unused)
68 0, // 0x8013 (Unused)
69 0, // 0x8014 (Unused)
70 0, // 0x8015 (Unused)
71 0, // 0x8016 (Unused)
72 16, // LF_OCTWORD
73 16, // LF_UOCTWORD
74 16, // LF_DECIMAL (sizeof(DECIMAL))
75 8, // LF_DATE (sizeof(DATE))
76 };
77
78 return 2 + Sizes[N - LF_NUMERIC];
79}
80
82 const char *S = reinterpret_cast<const char *>(Data.data());
83 return strlen(S) + 1;
84}
85
88 uint32_t Offset = 0;
89
90 while (!Content.empty()) {
91 // Array of:
92 // 0: Attrs
93 // 2: Padding
94 // 4: TypeIndex
95 // if (isIntroVirtual())
96 // 8: VFTableOffset
97
98 // At least 8 bytes are guaranteed. 4 extra bytes come iff function is an
99 // intro virtual.
100 uint32_t Len = 8;
101
102 uint16_t Attrs = support::endian::read16le(Content.data());
103 Refs.push_back({TiRefKind::TypeRef, Offset + 4, 1});
104
105 if (LLVM_UNLIKELY(isIntroVirtual(Attrs)))
106 Len += 4;
107 Offset += Len;
108 Content = Content.drop_front(Len);
109 }
110}
111
114 // 0: Kind
115 // 2: Padding
116 // 4: TypeIndex
117 // 8: Encoded Integer
118 Refs.push_back({TiRefKind::TypeRef, Offset + 4, 1});
119 return 8 + getEncodedIntegerLength(Data.drop_front(8));
120}
121
124 // 0: Kind
125 // 2: Padding
126 // 4: Encoded Integer
127 // <next>: Name
128 uint32_t Size = 4 + getEncodedIntegerLength(Data.drop_front(4));
129 return Size + getCStringLength(Data.drop_front(Size));
130}
131
134 // 0: Kind
135 // 2: Padding
136 // 4: TypeIndex
137 // 8: Encoded Integer
138 // <next>: Name
139 Refs.push_back({TiRefKind::TypeRef, Offset + 4, 1});
140 uint32_t Size = 8 + getEncodedIntegerLength(Data.drop_front(8));
141 return Size + getCStringLength(Data.drop_front(Size));
142}
143
146 // 0: Kind
147 // 2: Padding
148 // 4: TypeIndex
149 // 8: Name
150 Refs.push_back({TiRefKind::TypeRef, Offset + 4, 1});
151 return 8 + getCStringLength(Data.drop_front(8));
152}
153
156 // 0: Kind
157 // 2: Attributes
158 // 4: Type
159 // if (isIntroVirtual)
160 // 8: VFTableOffset
161 // <next>: Name
162 uint32_t Size = 8;
163 Refs.push_back({TiRefKind::TypeRef, Offset + 4, 1});
164
165 uint16_t Attrs = support::endian::read16le(Data.drop_front(2).data());
166 if (LLVM_UNLIKELY(isIntroVirtual(Attrs)))
167 Size += 4;
168
169 return Size + getCStringLength(Data.drop_front(Size));
170}
171
174 // 0: Kind
175 // 2: Padding
176 // 4: TypeIndex
177 // 8: Name
178 Refs.push_back({TiRefKind::TypeRef, Offset + 4, 1});
179 return 8 + getCStringLength(Data.drop_front(8));
180}
181
184 // 0: Kind
185 // 2: Padding
186 // 4: TypeIndex
187 // 8: Name
188 Refs.push_back({TiRefKind::TypeRef, Offset + 4, 1});
189 return 8 + getCStringLength(Data.drop_front(8));
190}
191
193 bool IsIndirect,
195 // 0: Kind
196 // 2: Attrs
197 // 4: TypeIndex
198 // 8: TypeIndex
199 // 12: Encoded Integer
200 // <next>: Encoded Integer
201 uint32_t Size = 12;
202 Refs.push_back({TiRefKind::TypeRef, Offset + 4, 2});
203 Size += getEncodedIntegerLength(Data.drop_front(Size));
204 Size += getEncodedIntegerLength(Data.drop_front(Size));
205 return Size;
206}
207
210 // 0: Kind
211 // 2: Padding
212 // 4: TypeIndex
213 Refs.push_back({TiRefKind::TypeRef, Offset + 4, 1});
214 return 8;
215}
216
219 // 0: Kind
220 // 2: Padding
221 // 4: TypeIndex
222 Refs.push_back({TiRefKind::TypeRef, Offset + 4, 1});
223 return 8;
224}
225
228 uint32_t Offset = 0;
229 uint32_t ThisLen = 0;
230 while (!Content.empty()) {
231 TypeLeafKind Kind =
232 static_cast<TypeLeafKind>(support::endian::read16le(Content.data()));
233 switch (Kind) {
234 case LF_BCLASS:
235 ThisLen = handleBaseClass(Content, Offset, Refs);
236 break;
237 case LF_ENUMERATE:
238 ThisLen = handleEnumerator(Content, Offset, Refs);
239 break;
240 case LF_MEMBER:
241 ThisLen = handleDataMember(Content, Offset, Refs);
242 break;
243 case LF_METHOD:
244 ThisLen = handleOverloadedMethod(Content, Offset, Refs);
245 break;
246 case LF_ONEMETHOD:
247 ThisLen = handleOneMethod(Content, Offset, Refs);
248 break;
249 case LF_NESTTYPE:
250 ThisLen = handleNestedType(Content, Offset, Refs);
251 break;
252 case LF_STMEMBER:
253 ThisLen = handleStaticDataMember(Content, Offset, Refs);
254 break;
255 case LF_VBCLASS:
256 case LF_IVBCLASS:
257 ThisLen =
258 handleVirtualBaseClass(Content, Offset, Kind == LF_VBCLASS, Refs);
259 break;
260 case LF_VFUNCTAB:
261 ThisLen = handleVFPtr(Content, Offset, Refs);
262 break;
263 case LF_INDEX:
264 ThisLen = handleListContinuation(Content, Offset, Refs);
265 break;
266 default:
267 return;
268 }
269 Content = Content.drop_front(ThisLen);
270 Offset += ThisLen;
271 if (!Content.empty()) {
272 uint8_t Pad = Content.front();
273 if (Pad >= LF_PAD0) {
274 uint32_t Skip = Pad & 0x0F;
275 Content = Content.drop_front(Skip);
276 Offset += Skip;
277 }
278 }
279 }
280}
281
284 Refs.push_back({TiRefKind::TypeRef, 0, 1});
285
286 uint32_t Attrs = support::endian::read32le(Content.drop_front(4).data());
287 if (isMemberPointer(Attrs))
288 Refs.push_back({TiRefKind::TypeRef, 8, 1});
289}
290
294 // FIXME: In the future it would be nice if we could avoid hardcoding these
295 // values. One idea is to define some structures representing these types
296 // that would allow the use of offsetof().
297 switch (Kind) {
298 case TypeLeafKind::LF_FUNC_ID:
299 Refs.push_back({TiRefKind::IndexRef, 0, 1});
300 Refs.push_back({TiRefKind::TypeRef, 4, 1});
301 break;
302 case TypeLeafKind::LF_MFUNC_ID:
303 Refs.push_back({TiRefKind::TypeRef, 0, 2});
304 break;
305 case TypeLeafKind::LF_STRING_ID:
306 Refs.push_back({TiRefKind::IndexRef, 0, 1});
307 break;
308 case TypeLeafKind::LF_SUBSTR_LIST:
310 if (Count > 0)
312 break;
313 case TypeLeafKind::LF_BUILDINFO:
315 if (Count > 0)
317 break;
318 case TypeLeafKind::LF_UDT_SRC_LINE:
319 Refs.push_back({TiRefKind::TypeRef, 0, 1});
320 Refs.push_back({TiRefKind::IndexRef, 4, 1});
321 break;
322 case TypeLeafKind::LF_UDT_MOD_SRC_LINE:
323 Refs.push_back({TiRefKind::TypeRef, 0, 1});
324 break;
325 case TypeLeafKind::LF_MODIFIER:
326 Refs.push_back({TiRefKind::TypeRef, 0, 1});
327 break;
328 case TypeLeafKind::LF_PROCEDURE:
329 Refs.push_back({TiRefKind::TypeRef, 0, 1});
330 Refs.push_back({TiRefKind::TypeRef, 8, 1});
331 break;
332 case TypeLeafKind::LF_MFUNCTION:
333 Refs.push_back({TiRefKind::TypeRef, 0, 3});
334 Refs.push_back({TiRefKind::TypeRef, 16, 1});
335 break;
336 case TypeLeafKind::LF_ARGLIST:
338 if (Count > 0)
340 break;
341 case TypeLeafKind::LF_ARRAY:
342 Refs.push_back({TiRefKind::TypeRef, 0, 2});
343 break;
344 case TypeLeafKind::LF_CLASS:
345 case TypeLeafKind::LF_STRUCTURE:
346 case TypeLeafKind::LF_INTERFACE:
347 case TypeLeafKind::LF_CLASS2:
348 case TypeLeafKind::LF_STRUCTURE2:
349 case TypeLeafKind::LF_INTERFACE2:
350 Refs.push_back({TiRefKind::TypeRef, 4, 3});
351 break;
352 case TypeLeafKind::LF_UNION:
353 case TypeLeafKind::LF_UNION2:
354 Refs.push_back({TiRefKind::TypeRef, 4, 1});
355 break;
356 case TypeLeafKind::LF_ENUM:
357 Refs.push_back({TiRefKind::TypeRef, 4, 2});
358 break;
359 case TypeLeafKind::LF_BITFIELD:
360 Refs.push_back({TiRefKind::TypeRef, 0, 1});
361 break;
362 case TypeLeafKind::LF_VFTABLE:
363 Refs.push_back({TiRefKind::TypeRef, 0, 2});
364 break;
365 case TypeLeafKind::LF_VTSHAPE:
366 break;
367 case TypeLeafKind::LF_METHODLIST:
368 handleMethodOverloadList(Content, Refs);
369 break;
370 case TypeLeafKind::LF_FIELDLIST:
371 handleFieldList(Content, Refs);
372 break;
373 case TypeLeafKind::LF_POINTER:
374 handlePointer(Content, Refs);
375 break;
376 default:
377 break;
378 }
379}
380
384 // FIXME: In the future it would be nice if we could avoid hardcoding these
385 // values. One idea is to define some structures representing these types
386 // that would allow the use of offsetof().
387 switch (Kind) {
388 case SymbolKind::S_GPROC32_ID:
389 case SymbolKind::S_LPROC32_ID:
390 case SymbolKind::S_LPROC32_DPC:
391 case SymbolKind::S_LPROC32_DPC_ID:
392 Refs.push_back({TiRefKind::IndexRef, 24, 1}); // LF_FUNC_ID
393 break;
394 case SymbolKind::S_GPROC32:
395 case SymbolKind::S_LPROC32:
396 Refs.push_back({TiRefKind::TypeRef, 24, 1}); // Type
397 break;
398 case SymbolKind::S_UDT:
399 Refs.push_back({TiRefKind::TypeRef, 0, 1}); // UDT
400 break;
401 case SymbolKind::S_GDATA32:
402 case SymbolKind::S_LDATA32:
403 Refs.push_back({TiRefKind::TypeRef, 0, 1}); // Type
404 break;
405 case SymbolKind::S_BUILDINFO:
406 Refs.push_back({TiRefKind::IndexRef, 0, 1}); // Compile flags
407 break;
408 case SymbolKind::S_LTHREAD32:
409 case SymbolKind::S_GTHREAD32:
410 Refs.push_back({TiRefKind::TypeRef, 0, 1}); // Type
411 break;
412 case SymbolKind::S_FILESTATIC:
413 Refs.push_back({TiRefKind::TypeRef, 0, 1}); // Type
414 break;
415 case SymbolKind::S_LOCAL:
416 Refs.push_back({TiRefKind::TypeRef, 0, 1}); // Type
417 break;
418 case SymbolKind::S_REGISTER:
419 Refs.push_back({TiRefKind::TypeRef, 0, 1}); // Type
420 break;
421 case SymbolKind::S_CONSTANT:
422 Refs.push_back({TiRefKind::TypeRef, 0, 1}); // Type
423 break;
424 case SymbolKind::S_BPREL32:
425 case SymbolKind::S_REGREL32:
426 case SymbolKind::S_REGREL32_INDIR:
427 Refs.push_back({TiRefKind::TypeRef, 4, 1}); // Type
428 break;
429 case SymbolKind::S_CALLSITEINFO:
430 Refs.push_back({TiRefKind::TypeRef, 8, 1}); // Call signature
431 break;
432 case SymbolKind::S_CALLERS:
433 case SymbolKind::S_CALLEES:
434 case SymbolKind::S_INLINEES:
435 // The record is a count followed by an array of type indices.
436 Count = *reinterpret_cast<const ulittle32_t *>(Content.data());
437 Refs.push_back({TiRefKind::IndexRef, 4, Count}); // Callees
438 break;
439 case SymbolKind::S_INLINESITE:
440 Refs.push_back({TiRefKind::IndexRef, 8, 1}); // ID of inlinee
441 break;
442 case SymbolKind::S_HEAPALLOCSITE:
443 Refs.push_back({TiRefKind::TypeRef, 8, 1}); // UDT allocated
444 break;
445
446 // Defranges don't have types, just registers and code offsets.
447 case SymbolKind::S_DEFRANGE_REGISTER:
448 case SymbolKind::S_DEFRANGE_REGISTER_REL:
449 case SymbolKind::S_DEFRANGE_REGISTER_REL_INDIR:
450 case SymbolKind::S_DEFRANGE_FRAMEPOINTER_REL:
451 case SymbolKind::S_DEFRANGE_FRAMEPOINTER_REL_FULL_SCOPE:
452 case SymbolKind::S_DEFRANGE_SUBFIELD_REGISTER:
453 case SymbolKind::S_DEFRANGE_SUBFIELD:
454 break;
455
456 // No type references.
457 case SymbolKind::S_LABEL32:
458 case SymbolKind::S_OBJNAME:
459 case SymbolKind::S_COMPILE:
460 case SymbolKind::S_COMPILE2:
461 case SymbolKind::S_COMPILE3:
462 case SymbolKind::S_ENVBLOCK:
463 case SymbolKind::S_BLOCK32:
464 case SymbolKind::S_FRAMEPROC:
465 case SymbolKind::S_THUNK32:
466 case SymbolKind::S_FRAMECOOKIE:
467 case SymbolKind::S_UNAMESPACE:
468 case SymbolKind::S_ARMSWITCHTABLE:
469 case SymbolKind::S_ASSOCIATION:
470 break;
471 // Scope ending symbols.
472 case SymbolKind::S_END:
473 case SymbolKind::S_INLINESITE_END:
474 case SymbolKind::S_PROC_ID_END:
475 break;
476 default:
477 return false; // Unknown symbol.
478 }
479 return true;
480}
481
486
490 Indices.clear();
491
492 if (Refs.empty())
493 return;
494
495 RecordData = RecordData.drop_front(sizeof(RecordPrefix));
496
498 for (const auto &Ref : Refs) {
499 Reader.setOffset(Ref.Offset);
501 cantFail(Reader.readArray(Run, Ref.Count));
502 Indices.append(Run.begin(), Run.end());
503 }
504}
505
508 return discoverTypeIndices(Type.RecordData, Indices);
509}
510
514 discoverTypeIndices(RecordData, Refs);
515 resolveTypeIndexReferences(RecordData, Refs, Indices);
516}
517
520 const RecordPrefix *P =
521 reinterpret_cast<const RecordPrefix *>(RecordData.data());
522 TypeLeafKind K = static_cast<TypeLeafKind>(uint16_t(P->RecordKind));
523 ::discoverTypeIndices(RecordData.drop_front(sizeof(RecordPrefix)), K, Refs);
524}
525
527 const CVSymbol &Sym, SmallVectorImpl<TiReference> &Refs) {
528 SymbolKind K = Sym.kind();
529 return ::discoverTypeIndices(Sym.content(), K, Refs);
530}
531
534 const RecordPrefix *P =
535 reinterpret_cast<const RecordPrefix *>(RecordData.data());
536 SymbolKind K = static_cast<SymbolKind>(uint16_t(P->RecordKind));
537 return ::discoverTypeIndices(RecordData.drop_front(sizeof(RecordPrefix)), K,
538 Refs);
539}
540
542 ArrayRef<uint8_t> RecordData, SmallVectorImpl<TypeIndex> &Indices) {
544 if (!discoverTypeIndicesInSymbol(RecordData, Refs))
545 return false;
546 resolveTypeIndexReferences(RecordData, Refs, Indices);
547 return true;
548}
#define LLVM_UNLIKELY(EXPR)
Definition Compiler.h:348
#define P(N)
static cl::opt< RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode > Mode("regalloc-enable-advisor", cl::Hidden, cl::init(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Default), cl::desc("Enable regalloc advisor mode"), cl::values(clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Default, "default", "Default"), clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Release, "release", "precompiled"), clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Development, "development", "for training")))
static uint32_t getCStringLength(ArrayRef< uint8_t > Data)
static uint32_t handleDataMember(ArrayRef< uint8_t > Data, uint32_t Offset, SmallVectorImpl< TiReference > &Refs)
static uint32_t handleListContinuation(ArrayRef< uint8_t > Data, uint32_t Offset, SmallVectorImpl< TiReference > &Refs)
static void resolveTypeIndexReferences(ArrayRef< uint8_t > RecordData, ArrayRef< TiReference > Refs, SmallVectorImpl< TypeIndex > &Indices)
static uint32_t handleEnumerator(ArrayRef< uint8_t > Data, uint32_t Offset, SmallVectorImpl< TiReference > &Refs)
static bool isMemberPointer(uint32_t Attrs)
static uint32_t handleNestedType(ArrayRef< uint8_t > Data, uint32_t Offset, SmallVectorImpl< TiReference > &Refs)
static uint32_t getEncodedIntegerLength(ArrayRef< uint8_t > Data)
static uint32_t handleVFPtr(ArrayRef< uint8_t > Data, uint32_t Offset, SmallVectorImpl< TiReference > &Refs)
static uint32_t handleOneMethod(ArrayRef< uint8_t > Data, uint32_t Offset, SmallVectorImpl< TiReference > &Refs)
static void handleFieldList(ArrayRef< uint8_t > Content, SmallVectorImpl< TiReference > &Refs)
static uint32_t handleBaseClass(ArrayRef< uint8_t > Data, uint32_t Offset, SmallVectorImpl< TiReference > &Refs)
static void handlePointer(ArrayRef< uint8_t > Content, SmallVectorImpl< TiReference > &Refs)
static void handleMethodOverloadList(ArrayRef< uint8_t > Content, SmallVectorImpl< TiReference > &Refs)
static uint32_t handleVirtualBaseClass(ArrayRef< uint8_t > Data, uint32_t Offset, bool IsIndirect, SmallVectorImpl< TiReference > &Refs)
static PointerMode getPointerMode(uint32_t Attrs)
static MethodKind getMethodKind(uint16_t Attrs)
static uint32_t handleOverloadedMethod(ArrayRef< uint8_t > Data, uint32_t Offset, SmallVectorImpl< TiReference > &Refs)
static bool isIntroVirtual(uint16_t Attrs)
static uint32_t handleStaticDataMember(ArrayRef< uint8_t > Data, uint32_t Offset, SmallVectorImpl< TiReference > &Refs)
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
ArrayRef< T > drop_front(size_t N=1) const
Drop the first N elements of the array.
Definition ArrayRef.h:194
const T & front() const
Get the first element.
Definition ArrayRef.h:144
bool empty() const
Check if the array is empty.
Definition ArrayRef.h:136
const T * data() const
Definition ArrayRef.h:138
Provides read only access to a subclass of BinaryStream.
Error readArray(ArrayRef< T > &Array, uint32_t NumElements)
Get a reference to a NumElements element array of objects of type T from the underlying stream as if ...
FixedStreamArray is similar to VarStreamArray, except with each record having a fixed-length.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
ArrayRef< uint8_t > content() const
Definition CVRecord.h:56
static const uint32_t PointerModeShift
Definition TypeRecord.h:275
static const uint32_t PointerModeMask
Definition TypeRecord.h:276
PointerMode
Equivalent to CV_ptrmode_e.
Definition CodeView.h:338
LLVM_ABI bool discoverTypeIndicesInSymbol(const CVSymbol &Symbol, SmallVectorImpl< TiReference > &Refs)
Discover type indices in symbol records.
MethodKind
Part of member attribute flags. (CV_methodprop_e)
Definition CodeView.h:255
CVRecord< TypeLeafKind > CVType
Definition CVRecord.h:64
LLVM_ABI void discoverTypeIndices(ArrayRef< uint8_t > RecordData, SmallVectorImpl< TiReference > &Refs)
CVRecord< SymbolKind > CVSymbol
Definition CVRecord.h:65
TypeLeafKind
Duplicate copy of the above enum, but using the official CV names.
Definition CodeView.h:34
SymbolKind
Duplicate copy of the above enum, but using the official CV names.
Definition CodeView.h:48
uint16_t read16le(const void *P)
Definition Endian.h:409
uint32_t read32le(const void *P)
Definition Endian.h:412
detail::packed_endian_specific_integral< uint32_t, llvm::endianness::little, unaligned > ulittle32_t
Definition Endian.h:270
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:577
@ Ref
The access may reference the value stored in memory.
Definition ModRef.h:32
void cantFail(Error Err, const char *Msg=nullptr)
Report a fatal error if Err is a failure value.
Definition Error.h:769
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Count
Definition InstrProf.h:145
#define N