1//===- BTFDebug.cpp - BTF Generator ---------------------------------------===//
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//
9// This file contains support for writing BTF debug info.
10//
11//===----------------------------------------------------------------------===//
12
13#include "BTFDebug.h"
14#include "BPF.h"
15#include "BPFCORE.h"
16#include "MCTargetDesc/BPFMCTargetDesc.h"
17#include "llvm/BinaryFormat/ELF.h"
18#include "llvm/CodeGen/AsmPrinter.h"
19#include "llvm/CodeGen/MachineModuleInfo.h"
20#include "llvm/CodeGen/MachineOperand.h"
21#include "llvm/MC/MCContext.h"
22#include "llvm/MC/MCObjectFileInfo.h"
23#include "llvm/MC/MCSectionELF.h"
24#include "llvm/MC/MCStreamer.h"
25#include "llvm/Support/LineIterator.h"
26#include "llvm/Support/MemoryBuffer.h"
27#include "llvm/Target/TargetLoweringObjectFile.h"
28#include <optional>
29
30using namespace llvm;
31
32static const char *BTFKindStr[] = {
33#define HANDLE_BTF_KIND(ID, NAME) "BTF_KIND_" #NAME,
34#include "llvm/DebugInfo/BTF/BTF.def"
35};
36
37/// Emit a BTF common type.
38void BTFTypeBase::emitType(MCStreamer &OS) {
39 OS.AddComment(T: std::string(BTFKindStr[Kind]) + "(id = " + std::to_string(val: Id) +
40 ")");
41 OS.emitInt32(Value: BTFType.NameOff);
42 OS.AddComment(T: "0x" + Twine::utohexstr(Val: BTFType.Info));
43 OS.emitInt32(Value: BTFType.Info);
44 OS.emitInt32(Value: BTFType.Size);
45}
46
47BTFTypeDerived::BTFTypeDerived(const DIDerivedType *DTy, unsigned Tag,
48 bool NeedsFixup)
49 : DTy(DTy), NeedsFixup(NeedsFixup), Name(DTy->getName()) {
50 switch (Tag) {
51 case dwarf::DW_TAG_pointer_type:
52 Kind = BTF::BTF_KIND_PTR;
53 break;
54 case dwarf::DW_TAG_const_type:
55 Kind = BTF::BTF_KIND_CONST;
56 break;
57 case dwarf::DW_TAG_volatile_type:
58 Kind = BTF::BTF_KIND_VOLATILE;
59 break;
60 case dwarf::DW_TAG_typedef:
61 Kind = BTF::BTF_KIND_TYPEDEF;
62 break;
63 case dwarf::DW_TAG_restrict_type:
64 Kind = BTF::BTF_KIND_RESTRICT;
65 break;
66 default:
67 llvm_unreachable("Unknown DIDerivedType Tag");
68 }
69 BTFType.Info = Kind << 24;
70}
71
72/// Used by DW_TAG_pointer_type only.
73BTFTypeDerived::BTFTypeDerived(unsigned NextTypeId, unsigned Tag,
74 StringRef Name)
75 : DTy(nullptr), NeedsFixup(false), Name(Name) {
76 Kind = BTF::BTF_KIND_PTR;
77 BTFType.Info = Kind << 24;
78 BTFType.Type = NextTypeId;
79}
80
81void BTFTypeDerived::completeType(BTFDebug &BDebug) {
82 if (IsCompleted)
83 return;
84 IsCompleted = true;
85
86 BTFType.NameOff = BDebug.addString(S: Name);
87
88 if (NeedsFixup || !DTy)
89 return;
90
91 // The base type for PTR/CONST/VOLATILE could be void.
92 const DIType *ResolvedType = DTy->getBaseType();
93 if (!ResolvedType) {
94 assert((Kind == BTF::BTF_KIND_PTR || Kind == BTF::BTF_KIND_CONST ||
95 Kind == BTF::BTF_KIND_VOLATILE) &&
96 "Invalid null basetype");
97 BTFType.Type = 0;
98 } else {
99 BTFType.Type = BDebug.getTypeId(Ty: ResolvedType);
100 }
101}
102
103void BTFTypeDerived::emitType(MCStreamer &OS) { BTFTypeBase::emitType(OS); }
104
105void BTFTypeDerived::setPointeeType(uint32_t PointeeType) {
106 BTFType.Type = PointeeType;
107}
108
109/// Represent a struct/union forward declaration.
110BTFTypeFwd::BTFTypeFwd(StringRef Name, bool IsUnion) : Name(Name) {
111 Kind = BTF::BTF_KIND_FWD;
112 BTFType.Info = IsUnion << 31 | Kind << 24;
113 BTFType.Type = 0;
114}
115
116void BTFTypeFwd::completeType(BTFDebug &BDebug) {
117 if (IsCompleted)
118 return;
119 IsCompleted = true;
120
121 BTFType.NameOff = BDebug.addString(S: Name);
122}
123
124void BTFTypeFwd::emitType(MCStreamer &OS) { BTFTypeBase::emitType(OS); }
125
126BTFTypeInt::BTFTypeInt(uint32_t Encoding, uint32_t SizeInBits,
127 uint32_t OffsetInBits, StringRef TypeName)
128 : Name(TypeName) {
129 // Translate IR int encoding to BTF int encoding.
130 uint8_t BTFEncoding;
131 switch (Encoding) {
132 case dwarf::DW_ATE_boolean:
133 BTFEncoding = BTF::INT_BOOL;
134 break;
135 case dwarf::DW_ATE_signed:
136 case dwarf::DW_ATE_signed_char:
137 BTFEncoding = BTF::INT_SIGNED;
138 break;
139 case dwarf::DW_ATE_unsigned:
140 case dwarf::DW_ATE_unsigned_char:
141 BTFEncoding = 0;
142 break;
143 default:
144 llvm_unreachable("Unknown BTFTypeInt Encoding");
145 }
146
147 Kind = BTF::BTF_KIND_INT;
148 BTFType.Info = Kind << 24;
149 BTFType.Size = roundupToBytes(NumBits: SizeInBits);
150 IntVal = (BTFEncoding << 24) | OffsetInBits << 16 | SizeInBits;
151}
152
153void BTFTypeInt::completeType(BTFDebug &BDebug) {
154 if (IsCompleted)
155 return;
156 IsCompleted = true;
157
158 BTFType.NameOff = BDebug.addString(S: Name);
159}
160
161void BTFTypeInt::emitType(MCStreamer &OS) {
162 BTFTypeBase::emitType(OS);
163 OS.AddComment(T: "0x" + Twine::utohexstr(Val: IntVal));
164 OS.emitInt32(Value: IntVal);
165}
166
167BTFTypeEnum::BTFTypeEnum(const DICompositeType *ETy, uint32_t VLen,
168 bool IsSigned) : ETy(ETy) {
169 Kind = BTF::BTF_KIND_ENUM;
170 BTFType.Info = IsSigned << 31 | Kind << 24 | VLen;
171 BTFType.Size = roundupToBytes(NumBits: ETy->getSizeInBits());
172}
173
174void BTFTypeEnum::completeType(BTFDebug &BDebug) {
175 if (IsCompleted)
176 return;
177 IsCompleted = true;
178
179 BTFType.NameOff = BDebug.addString(S: ETy->getName());
180
181 DINodeArray Elements = ETy->getElements();
182 for (const auto Element : Elements) {
183 const auto *Enum = cast<DIEnumerator>(Val: Element);
184
185 struct BTF::BTFEnum BTFEnum;
186 BTFEnum.NameOff = BDebug.addString(S: Enum->getName());
187 // BTF enum value is 32bit, enforce it.
188 uint32_t Value;
189 if (Enum->isUnsigned())
190 Value = static_cast<uint32_t>(Enum->getValue().getZExtValue());
191 else
192 Value = static_cast<uint32_t>(Enum->getValue().getSExtValue());
193 BTFEnum.Val = Value;
194 EnumValues.push_back(x: BTFEnum);
195 }
196}
197
198void BTFTypeEnum::emitType(MCStreamer &OS) {
199 BTFTypeBase::emitType(OS);
200 for (const auto &Enum : EnumValues) {
201 OS.emitInt32(Value: Enum.NameOff);
202 OS.emitInt32(Value: Enum.Val);
203 }
204}
205
206BTFTypeEnum64::BTFTypeEnum64(const DICompositeType *ETy, uint32_t VLen,
207 bool IsSigned) : ETy(ETy) {
208 Kind = BTF::BTF_KIND_ENUM64;
209 BTFType.Info = IsSigned << 31 | Kind << 24 | VLen;
210 BTFType.Size = roundupToBytes(NumBits: ETy->getSizeInBits());
211}
212
213void BTFTypeEnum64::completeType(BTFDebug &BDebug) {
214 if (IsCompleted)
215 return;
216 IsCompleted = true;
217
218 BTFType.NameOff = BDebug.addString(S: ETy->getName());
219
220 DINodeArray Elements = ETy->getElements();
221 for (const auto Element : Elements) {
222 const auto *Enum = cast<DIEnumerator>(Val: Element);
223
224 struct BTF::BTFEnum64 BTFEnum;
225 BTFEnum.NameOff = BDebug.addString(S: Enum->getName());
226 uint64_t Value;
227 if (Enum->isUnsigned())
228 Value = static_cast<uint64_t>(Enum->getValue().getZExtValue());
229 else
230 Value = static_cast<uint64_t>(Enum->getValue().getSExtValue());
231 BTFEnum.Val_Lo32 = Value;
232 BTFEnum.Val_Hi32 = Value >> 32;
233 EnumValues.push_back(x: BTFEnum);
234 }
235}
236
237void BTFTypeEnum64::emitType(MCStreamer &OS) {
238 BTFTypeBase::emitType(OS);
239 for (const auto &Enum : EnumValues) {
240 OS.emitInt32(Value: Enum.NameOff);
241 OS.AddComment(T: "0x" + Twine::utohexstr(Val: Enum.Val_Lo32));
242 OS.emitInt32(Value: Enum.Val_Lo32);
243 OS.AddComment(T: "0x" + Twine::utohexstr(Val: Enum.Val_Hi32));
244 OS.emitInt32(Value: Enum.Val_Hi32);
245 }
246}
247
248BTFTypeArray::BTFTypeArray(uint32_t ElemTypeId, uint32_t NumElems) {
249 Kind = BTF::BTF_KIND_ARRAY;
250 BTFType.NameOff = 0;
251 BTFType.Info = Kind << 24;
252 BTFType.Size = 0;
253
254 ArrayInfo.ElemType = ElemTypeId;
255 ArrayInfo.Nelems = NumElems;
256}
257
258/// Represent a BTF array.
259void BTFTypeArray::completeType(BTFDebug &BDebug) {
260 if (IsCompleted)
261 return;
262 IsCompleted = true;
263
264 // The IR does not really have a type for the index.
265 // A special type for array index should have been
266 // created during initial type traversal. Just
267 // retrieve that type id.
268 ArrayInfo.IndexType = BDebug.getArrayIndexTypeId();
269}
270
271void BTFTypeArray::emitType(MCStreamer &OS) {
272 BTFTypeBase::emitType(OS);
273 OS.emitInt32(Value: ArrayInfo.ElemType);
274 OS.emitInt32(Value: ArrayInfo.IndexType);
275 OS.emitInt32(Value: ArrayInfo.Nelems);
276}
277
278/// Represent either a struct or a union.
279BTFTypeStruct::BTFTypeStruct(const DICompositeType *STy, bool IsStruct,
280 bool HasBitField, uint32_t Vlen)
281 : STy(STy), HasBitField(HasBitField) {
282 Kind = IsStruct ? BTF::BTF_KIND_STRUCT : BTF::BTF_KIND_UNION;
283 BTFType.Size = roundupToBytes(NumBits: STy->getSizeInBits());
284 BTFType.Info = (HasBitField << 31) | (Kind << 24) | Vlen;
285}
286
287void BTFTypeStruct::completeType(BTFDebug &BDebug) {
288 if (IsCompleted)
289 return;
290 IsCompleted = true;
291
292 BTFType.NameOff = BDebug.addString(S: STy->getName());
293
294 // Add struct/union members.
295 const DINodeArray Elements = STy->getElements();
296 for (const auto *Element : Elements) {
297 struct BTF::BTFMember BTFMember;
298 const auto *DDTy = cast<DIDerivedType>(Val: Element);
299
300 BTFMember.NameOff = BDebug.addString(S: DDTy->getName());
301 if (HasBitField) {
302 uint8_t BitFieldSize = DDTy->isBitField() ? DDTy->getSizeInBits() : 0;
303 BTFMember.Offset = BitFieldSize << 24 | DDTy->getOffsetInBits();
304 } else {
305 BTFMember.Offset = DDTy->getOffsetInBits();
306 }
307 const auto *BaseTy = DDTy->getBaseType();
308 BTFMember.Type = BDebug.getTypeId(Ty: BaseTy);
309 Members.push_back(x: BTFMember);
310 }
311}
312
313void BTFTypeStruct::emitType(MCStreamer &OS) {
314 BTFTypeBase::emitType(OS);
315 for (const auto &Member : Members) {
316 OS.emitInt32(Value: Member.NameOff);
317 OS.emitInt32(Value: Member.Type);
318 OS.AddComment(T: "0x" + Twine::utohexstr(Val: Member.Offset));
319 OS.emitInt32(Value: Member.Offset);
320 }
321}
322
323std::string BTFTypeStruct::getName() { return std::string(STy->getName()); }
324
325/// The Func kind represents both subprogram and pointee of function
326/// pointers. If the FuncName is empty, it represents a pointee of function
327/// pointer. Otherwise, it represents a subprogram. The func arg names
328/// are empty for pointee of function pointer case, and are valid names
329/// for subprogram.
330BTFTypeFuncProto::BTFTypeFuncProto(
331 const DISubroutineType *STy, uint32_t VLen,
332 const std::unordered_map<uint32_t, StringRef> &FuncArgNames)
333 : STy(STy), FuncArgNames(FuncArgNames) {
334 Kind = BTF::BTF_KIND_FUNC_PROTO;
335 BTFType.Info = (Kind << 24) | VLen;
336}
337
338void BTFTypeFuncProto::completeType(BTFDebug &BDebug) {
339 if (IsCompleted)
340 return;
341 IsCompleted = true;
342
343 DITypeRefArray Elements = STy->getTypeArray();
344 auto RetType = Elements[0];
345 BTFType.Type = RetType ? BDebug.getTypeId(Ty: RetType) : 0;
346 BTFType.NameOff = 0;
347
348 // For null parameter which is typically the last one
349 // to represent the vararg, encode the NameOff/Type to be 0.
350 for (unsigned I = 1, N = Elements.size(); I < N; ++I) {
351 struct BTF::BTFParam Param;
352 auto Element = Elements[I];
353 if (Element) {
354 Param.NameOff = BDebug.addString(S: FuncArgNames[I]);
355 Param.Type = BDebug.getTypeId(Ty: Element);
356 } else {
357 Param.NameOff = 0;
358 Param.Type = 0;
359 }
360 Parameters.push_back(x: Param);
361 }
362}
363
364void BTFTypeFuncProto::emitType(MCStreamer &OS) {
365 BTFTypeBase::emitType(OS);
366 for (const auto &Param : Parameters) {
367 OS.emitInt32(Value: Param.NameOff);
368 OS.emitInt32(Value: Param.Type);
369 }
370}
371
372BTFTypeFunc::BTFTypeFunc(StringRef FuncName, uint32_t ProtoTypeId,
373 uint32_t Scope)
374 : Name(FuncName) {
375 Kind = BTF::BTF_KIND_FUNC;
376 BTFType.Info = (Kind << 24) | Scope;
377 BTFType.Type = ProtoTypeId;
378}
379
380void BTFTypeFunc::completeType(BTFDebug &BDebug) {
381 if (IsCompleted)
382 return;
383 IsCompleted = true;
384
385 BTFType.NameOff = BDebug.addString(S: Name);
386}
387
388void BTFTypeFunc::emitType(MCStreamer &OS) { BTFTypeBase::emitType(OS); }
389
390BTFKindVar::BTFKindVar(StringRef VarName, uint32_t TypeId, uint32_t VarInfo)
391 : Name(VarName) {
392 Kind = BTF::BTF_KIND_VAR;
393 BTFType.Info = Kind << 24;
394 BTFType.Type = TypeId;
395 Info = VarInfo;
396}
397
398void BTFKindVar::completeType(BTFDebug &BDebug) {
399 BTFType.NameOff = BDebug.addString(S: Name);
400}
401
402void BTFKindVar::emitType(MCStreamer &OS) {
403 BTFTypeBase::emitType(OS);
404 OS.emitInt32(Value: Info);
405}
406
407BTFKindDataSec::BTFKindDataSec(AsmPrinter *AsmPrt, std::string SecName)
408 : Asm(AsmPrt), Name(SecName) {
409 Kind = BTF::BTF_KIND_DATASEC;
410 BTFType.Info = Kind << 24;
411 BTFType.Size = 0;
412}
413
414void BTFKindDataSec::completeType(BTFDebug &BDebug) {
415 BTFType.NameOff = BDebug.addString(S: Name);
416 BTFType.Info |= Vars.size();
417}
418
419void BTFKindDataSec::emitType(MCStreamer &OS) {
420 BTFTypeBase::emitType(OS);
421
422 for (const auto &V : Vars) {
423 OS.emitInt32(Value: std::get<0>(t: V));
424 Asm->emitLabelReference(Label: std::get<1>(t: V), Size: 4);
425 OS.emitInt32(Value: std::get<2>(t: V));
426 }
427}
428
429BTFTypeFloat::BTFTypeFloat(uint32_t SizeInBits, StringRef TypeName)
430 : Name(TypeName) {
431 Kind = BTF::BTF_KIND_FLOAT;
432 BTFType.Info = Kind << 24;
433 BTFType.Size = roundupToBytes(NumBits: SizeInBits);
434}
435
436void BTFTypeFloat::completeType(BTFDebug &BDebug) {
437 if (IsCompleted)
438 return;
439 IsCompleted = true;
440
441 BTFType.NameOff = BDebug.addString(S: Name);
442}
443
444BTFTypeDeclTag::BTFTypeDeclTag(uint32_t BaseTypeId, int ComponentIdx,
445 StringRef Tag)
446 : Tag(Tag) {
447 Kind = BTF::BTF_KIND_DECL_TAG;
448 BTFType.Info = Kind << 24;
449 BTFType.Type = BaseTypeId;
450 Info = ComponentIdx;
451}
452
453void BTFTypeDeclTag::completeType(BTFDebug &BDebug) {
454 if (IsCompleted)
455 return;
456 IsCompleted = true;
457
458 BTFType.NameOff = BDebug.addString(S: Tag);
459}
460
461void BTFTypeDeclTag::emitType(MCStreamer &OS) {
462 BTFTypeBase::emitType(OS);
463 OS.emitInt32(Value: Info);
464}
465
466BTFTypeTypeTag::BTFTypeTypeTag(uint32_t NextTypeId, StringRef Tag)
467 : DTy(nullptr), Tag(Tag) {
468 Kind = BTF::BTF_KIND_TYPE_TAG;
469 BTFType.Info = Kind << 24;
470 BTFType.Type = NextTypeId;
471}
472
473BTFTypeTypeTag::BTFTypeTypeTag(const DIDerivedType *DTy, StringRef Tag)
474 : DTy(DTy), Tag(Tag) {
475 Kind = BTF::BTF_KIND_TYPE_TAG;
476 BTFType.Info = Kind << 24;
477}
478
479void BTFTypeTypeTag::completeType(BTFDebug &BDebug) {
480 if (IsCompleted)
481 return;
482 IsCompleted = true;
483 BTFType.NameOff = BDebug.addString(S: Tag);
484 if (DTy) {
485 const DIType *ResolvedType = DTy->getBaseType();
486 if (!ResolvedType)
487 BTFType.Type = 0;
488 else
489 BTFType.Type = BDebug.getTypeId(Ty: ResolvedType);
490 }
491}
492
493uint32_t BTFStringTable::addString(StringRef S) {
494 // Check whether the string already exists.
495 for (auto &OffsetM : OffsetToIdMap) {
496 if (Table[OffsetM.second] == S)
497 return OffsetM.first;
498 }
499 // Not find, add to the string table.
500 uint32_t Offset = Size;
501 OffsetToIdMap[Offset] = Table.size();
502 Table.push_back(x: std::string(S));
503 Size += S.size() + 1;
504 return Offset;
505}
506
507BTFDebug::BTFDebug(AsmPrinter *AP)
508 : DebugHandlerBase(AP), OS(*Asm->OutStreamer), SkipInstruction(false),
509 LineInfoGenerated(false), SecNameOff(0), ArrayIndexTypeId(0),
510 MapDefNotCollected(true) {
511 addString(S: "\0");
512}
513
514uint32_t BTFDebug::addType(std::unique_ptr<BTFTypeBase> TypeEntry,
515 const DIType *Ty) {
516 TypeEntry->setId(TypeEntries.size() + 1);
517 uint32_t Id = TypeEntry->getId();
518 DIToIdMap[Ty] = Id;
519 TypeEntries.push_back(x: std::move(TypeEntry));
520 return Id;
521}
522
523uint32_t BTFDebug::addType(std::unique_ptr<BTFTypeBase> TypeEntry) {
524 TypeEntry->setId(TypeEntries.size() + 1);
525 uint32_t Id = TypeEntry->getId();
526 TypeEntries.push_back(x: std::move(TypeEntry));
527 return Id;
528}
529
530void BTFDebug::visitBasicType(const DIBasicType *BTy, uint32_t &TypeId) {
531 // Only int and binary floating point types are supported in BTF.
532 uint32_t Encoding = BTy->getEncoding();
533 std::unique_ptr<BTFTypeBase> TypeEntry;
534 switch (Encoding) {
535 case dwarf::DW_ATE_boolean:
536 case dwarf::DW_ATE_signed:
537 case dwarf::DW_ATE_signed_char:
538 case dwarf::DW_ATE_unsigned:
539 case dwarf::DW_ATE_unsigned_char:
540 // Create a BTF type instance for this DIBasicType and put it into
541 // DIToIdMap for cross-type reference check.
542 TypeEntry = std::make_unique<BTFTypeInt>(
543 args&: Encoding, args: BTy->getSizeInBits(), args: BTy->getOffsetInBits(), args: BTy->getName());
544 break;
545 case dwarf::DW_ATE_float:
546 TypeEntry =
547 std::make_unique<BTFTypeFloat>(args: BTy->getSizeInBits(), args: BTy->getName());
548 break;
549 default:
550 return;
551 }
552
553 TypeId = addType(TypeEntry: std::move(TypeEntry), Ty: BTy);
554}
555
556/// Handle subprogram or subroutine types.
557void BTFDebug::visitSubroutineType(
558 const DISubroutineType *STy, bool ForSubprog,
559 const std::unordered_map<uint32_t, StringRef> &FuncArgNames,
560 uint32_t &TypeId) {
561 DITypeRefArray Elements = STy->getTypeArray();
562 uint32_t VLen = Elements.size() - 1;
563 if (VLen > BTF::MAX_VLEN)
564 return;
565
566 // Subprogram has a valid non-zero-length name, and the pointee of
567 // a function pointer has an empty name. The subprogram type will
568 // not be added to DIToIdMap as it should not be referenced by
569 // any other types.
570 auto TypeEntry = std::make_unique<BTFTypeFuncProto>(args&: STy, args&: VLen, args: FuncArgNames);
571 if (ForSubprog)
572 TypeId = addType(TypeEntry: std::move(TypeEntry)); // For subprogram
573 else
574 TypeId = addType(TypeEntry: std::move(TypeEntry), Ty: STy); // For func ptr
575
576 // Visit return type and func arg types.
577 for (const auto Element : Elements) {
578 visitTypeEntry(Ty: Element);
579 }
580}
581
582void BTFDebug::processDeclAnnotations(DINodeArray Annotations,
583 uint32_t BaseTypeId,
584 int ComponentIdx) {
585 if (!Annotations)
586 return;
587
588 for (const Metadata *Annotation : Annotations->operands()) {
589 const MDNode *MD = cast<MDNode>(Val: Annotation);
590 const MDString *Name = cast<MDString>(Val: MD->getOperand(I: 0));
591 if (!Name->getString().equals(RHS: "btf_decl_tag"))
592 continue;
593
594 const MDString *Value = cast<MDString>(Val: MD->getOperand(I: 1));
595 auto TypeEntry = std::make_unique<BTFTypeDeclTag>(args&: BaseTypeId, args&: ComponentIdx,
596 args: Value->getString());
597 addType(TypeEntry: std::move(TypeEntry));
598 }
599}
600
601uint32_t BTFDebug::processDISubprogram(const DISubprogram *SP,
602 uint32_t ProtoTypeId, uint8_t Scope) {
603 auto FuncTypeEntry =
604 std::make_unique<BTFTypeFunc>(args: SP->getName(), args&: ProtoTypeId, args&: Scope);
605 uint32_t FuncId = addType(TypeEntry: std::move(FuncTypeEntry));
606
607 // Process argument annotations.
608 for (const DINode *DN : SP->getRetainedNodes()) {
609 if (const auto *DV = dyn_cast<DILocalVariable>(Val: DN)) {
610 uint32_t Arg = DV->getArg();
611 if (Arg)
612 processDeclAnnotations(Annotations: DV->getAnnotations(), BaseTypeId: FuncId, ComponentIdx: Arg - 1);
613 }
614 }
615 processDeclAnnotations(Annotations: SP->getAnnotations(), BaseTypeId: FuncId, ComponentIdx: -1);
616
617 return FuncId;
618}
619
620/// Generate btf_type_tag chains.
621int BTFDebug::genBTFTypeTags(const DIDerivedType *DTy, int BaseTypeId) {
622 SmallVector<const MDString *, 4> MDStrs;
623 DINodeArray Annots = DTy->getAnnotations();
624 if (Annots) {
625 // For type with "int __tag1 __tag2 *p", the MDStrs will have
626 // content: [__tag1, __tag2].
627 for (const Metadata *Annotations : Annots->operands()) {
628 const MDNode *MD = cast<MDNode>(Val: Annotations);
629 const MDString *Name = cast<MDString>(Val: MD->getOperand(I: 0));
630 if (!Name->getString().equals(RHS: "btf_type_tag"))
631 continue;
632 MDStrs.push_back(Elt: cast<MDString>(Val: MD->getOperand(I: 1)));
633 }
634 }
635
636 if (MDStrs.size() == 0)
637 return -1;
638
639 // With MDStrs [__tag1, __tag2], the output type chain looks like
640 // PTR -> __tag2 -> __tag1 -> BaseType
641 // In the below, we construct BTF types with the order of __tag1, __tag2
642 // and PTR.
643 unsigned TmpTypeId;
644 std::unique_ptr<BTFTypeTypeTag> TypeEntry;
645 if (BaseTypeId >= 0)
646 TypeEntry =
647 std::make_unique<BTFTypeTypeTag>(args&: BaseTypeId, args: MDStrs[0]->getString());
648 else
649 TypeEntry = std::make_unique<BTFTypeTypeTag>(args&: DTy, args: MDStrs[0]->getString());
650 TmpTypeId = addType(TypeEntry: std::move(TypeEntry));
651
652 for (unsigned I = 1; I < MDStrs.size(); I++) {
653 const MDString *Value = MDStrs[I];
654 TypeEntry = std::make_unique<BTFTypeTypeTag>(args&: TmpTypeId, args: Value->getString());
655 TmpTypeId = addType(TypeEntry: std::move(TypeEntry));
656 }
657 return TmpTypeId;
658}
659
660/// Handle structure/union types.
661void BTFDebug::visitStructType(const DICompositeType *CTy, bool IsStruct,
662 uint32_t &TypeId) {
663 const DINodeArray Elements = CTy->getElements();
664 uint32_t VLen = Elements.size();
665 if (VLen > BTF::MAX_VLEN)
666 return;
667
668 // Check whether we have any bitfield members or not
669 bool HasBitField = false;
670 for (const auto *Element : Elements) {
671 auto E = cast<DIDerivedType>(Val: Element);
672 if (E->isBitField()) {
673 HasBitField = true;
674 break;
675 }
676 }
677
678 auto TypeEntry =
679 std::make_unique<BTFTypeStruct>(args&: CTy, args&: IsStruct, args&: HasBitField, args&: VLen);
680 StructTypes.push_back(x: TypeEntry.get());
681 TypeId = addType(TypeEntry: std::move(TypeEntry), Ty: CTy);
682
683 // Check struct/union annotations
684 processDeclAnnotations(Annotations: CTy->getAnnotations(), BaseTypeId: TypeId, ComponentIdx: -1);
685
686 // Visit all struct members.
687 int FieldNo = 0;
688 for (const auto *Element : Elements) {
689 const auto Elem = cast<DIDerivedType>(Val: Element);
690 visitTypeEntry(Ty: Elem);
691 processDeclAnnotations(Annotations: Elem->getAnnotations(), BaseTypeId: TypeId, ComponentIdx: FieldNo);
692 FieldNo++;
693 }
694}
695
696void BTFDebug::visitArrayType(const DICompositeType *CTy, uint32_t &TypeId) {
697 // Visit array element type.
698 uint32_t ElemTypeId;
699 const DIType *ElemType = CTy->getBaseType();
700 visitTypeEntry(Ty: ElemType, TypeId&: ElemTypeId, CheckPointer: false, SeenPointer: false);
701
702 // Visit array dimensions.
703 DINodeArray Elements = CTy->getElements();
704 for (int I = Elements.size() - 1; I >= 0; --I) {
705 if (auto *Element = dyn_cast_or_null<DINode>(Val: Elements[I]))
706 if (Element->getTag() == dwarf::DW_TAG_subrange_type) {
707 const DISubrange *SR = cast<DISubrange>(Val: Element);
708 auto *CI = SR->getCount().dyn_cast<ConstantInt *>();
709 int64_t Count = CI->getSExtValue();
710
711 // For struct s { int b; char c[]; }, the c[] will be represented
712 // as an array with Count = -1.
713 auto TypeEntry =
714 std::make_unique<BTFTypeArray>(args&: ElemTypeId,
715 args: Count >= 0 ? Count : 0);
716 if (I == 0)
717 ElemTypeId = addType(TypeEntry: std::move(TypeEntry), Ty: CTy);
718 else
719 ElemTypeId = addType(TypeEntry: std::move(TypeEntry));
720 }
721 }
722
723 // The array TypeId is the type id of the outermost dimension.
724 TypeId = ElemTypeId;
725
726 // The IR does not have a type for array index while BTF wants one.
727 // So create an array index type if there is none.
728 if (!ArrayIndexTypeId) {
729 auto TypeEntry = std::make_unique<BTFTypeInt>(args: dwarf::DW_ATE_unsigned, args: 32,
730 args: 0, args: "__ARRAY_SIZE_TYPE__");
731 ArrayIndexTypeId = addType(TypeEntry: std::move(TypeEntry));
732 }
733}
734
735void BTFDebug::visitEnumType(const DICompositeType *CTy, uint32_t &TypeId) {
736 DINodeArray Elements = CTy->getElements();
737 uint32_t VLen = Elements.size();
738 if (VLen > BTF::MAX_VLEN)
739 return;
740
741 bool IsSigned = false;
742 unsigned NumBits = 32;
743 // No BaseType implies forward declaration in which case a
744 // BTFTypeEnum with Vlen = 0 is emitted.
745 if (CTy->getBaseType() != nullptr) {
746 const auto *BTy = cast<DIBasicType>(Val: CTy->getBaseType());
747 IsSigned = BTy->getEncoding() == dwarf::DW_ATE_signed ||
748 BTy->getEncoding() == dwarf::DW_ATE_signed_char;
749 NumBits = BTy->getSizeInBits();
750 }
751
752 if (NumBits <= 32) {
753 auto TypeEntry = std::make_unique<BTFTypeEnum>(args&: CTy, args&: VLen, args&: IsSigned);
754 TypeId = addType(TypeEntry: std::move(TypeEntry), Ty: CTy);
755 } else {
756 assert(NumBits == 64);
757 auto TypeEntry = std::make_unique<BTFTypeEnum64>(args&: CTy, args&: VLen, args&: IsSigned);
758 TypeId = addType(TypeEntry: std::move(TypeEntry), Ty: CTy);
759 }
760 // No need to visit base type as BTF does not encode it.
761}
762
763/// Handle structure/union forward declarations.
764void BTFDebug::visitFwdDeclType(const DICompositeType *CTy, bool IsUnion,
765 uint32_t &TypeId) {
766 auto TypeEntry = std::make_unique<BTFTypeFwd>(args: CTy->getName(), args&: IsUnion);
767 TypeId = addType(TypeEntry: std::move(TypeEntry), Ty: CTy);
768}
769
770/// Handle structure, union, array and enumeration types.
771void BTFDebug::visitCompositeType(const DICompositeType *CTy,
772 uint32_t &TypeId) {
773 auto Tag = CTy->getTag();
774 if (Tag == dwarf::DW_TAG_structure_type || Tag == dwarf::DW_TAG_union_type) {
775 // Handle forward declaration differently as it does not have members.
776 if (CTy->isForwardDecl())
777 visitFwdDeclType(CTy, IsUnion: Tag == dwarf::DW_TAG_union_type, TypeId);
778 else
779 visitStructType(CTy, IsStruct: Tag == dwarf::DW_TAG_structure_type, TypeId);
780 } else if (Tag == dwarf::DW_TAG_array_type)
781 visitArrayType(CTy, TypeId);
782 else if (Tag == dwarf::DW_TAG_enumeration_type)
783 visitEnumType(CTy, TypeId);
784}
785
786bool BTFDebug::IsForwardDeclCandidate(const DIType *Base) {
787 if (const auto *CTy = dyn_cast<DICompositeType>(Val: Base)) {
788 auto CTag = CTy->getTag();
789 if ((CTag == dwarf::DW_TAG_structure_type ||
790 CTag == dwarf::DW_TAG_union_type) &&
791 !CTy->getName().empty() && !CTy->isForwardDecl())
792 return true;
793 }
794 return false;
795}
796
797/// Handle pointer, typedef, const, volatile, restrict and member types.
798void BTFDebug::visitDerivedType(const DIDerivedType *DTy, uint32_t &TypeId,
799 bool CheckPointer, bool SeenPointer) {
800 unsigned Tag = DTy->getTag();
801
802 /// Try to avoid chasing pointees, esp. structure pointees which may
803 /// unnecessary bring in a lot of types.
804 if (CheckPointer && !SeenPointer) {
805 SeenPointer = Tag == dwarf::DW_TAG_pointer_type;
806 }
807
808 if (CheckPointer && SeenPointer) {
809 const DIType *Base = DTy->getBaseType();
810 if (Base) {
811 if (IsForwardDeclCandidate(Base)) {
812 /// Find a candidate, generate a fixup. Later on the struct/union
813 /// pointee type will be replaced with either a real type or
814 /// a forward declaration.
815 auto TypeEntry = std::make_unique<BTFTypeDerived>(args&: DTy, args&: Tag, args: true);
816 auto &Fixup = FixupDerivedTypes[cast<DICompositeType>(Val: Base)];
817 Fixup.push_back(x: std::make_pair(x&: DTy, y: TypeEntry.get()));
818 TypeId = addType(TypeEntry: std::move(TypeEntry), Ty: DTy);
819 return;
820 }
821 }
822 }
823
824 if (Tag == dwarf::DW_TAG_pointer_type) {
825 int TmpTypeId = genBTFTypeTags(DTy, BaseTypeId: -1);
826 if (TmpTypeId >= 0) {
827 auto TypeDEntry =
828 std::make_unique<BTFTypeDerived>(args&: TmpTypeId, args&: Tag, args: DTy->getName());
829 TypeId = addType(TypeEntry: std::move(TypeDEntry), Ty: DTy);
830 } else {
831 auto TypeEntry = std::make_unique<BTFTypeDerived>(args&: DTy, args&: Tag, args: false);
832 TypeId = addType(TypeEntry: std::move(TypeEntry), Ty: DTy);
833 }
834 } else if (Tag == dwarf::DW_TAG_typedef || Tag == dwarf::DW_TAG_const_type ||
835 Tag == dwarf::DW_TAG_volatile_type ||
836 Tag == dwarf::DW_TAG_restrict_type) {
837 auto TypeEntry = std::make_unique<BTFTypeDerived>(args&: DTy, args&: Tag, args: false);
838 TypeId = addType(TypeEntry: std::move(TypeEntry), Ty: DTy);
839 if (Tag == dwarf::DW_TAG_typedef)
840 processDeclAnnotations(Annotations: DTy->getAnnotations(), BaseTypeId: TypeId, ComponentIdx: -1);
841 } else if (Tag != dwarf::DW_TAG_member) {
842 return;
843 }
844
845 // Visit base type of pointer, typedef, const, volatile, restrict or
846 // struct/union member.
847 uint32_t TempTypeId = 0;
848 if (Tag == dwarf::DW_TAG_member)
849 visitTypeEntry(Ty: DTy->getBaseType(), TypeId&: TempTypeId, CheckPointer: true, SeenPointer: false);
850 else
851 visitTypeEntry(Ty: DTy->getBaseType(), TypeId&: TempTypeId, CheckPointer, SeenPointer);
852}
853
854/// Visit a type entry. CheckPointer is true if the type has
855/// one of its predecessors as one struct/union member. SeenPointer
856/// is true if CheckPointer is true and one of its predecessors
857/// is a pointer. The goal of CheckPointer and SeenPointer is to
858/// do pruning for struct/union types so some of these types
859/// will not be emitted in BTF and rather forward declarations
860/// will be generated.
861void BTFDebug::visitTypeEntry(const DIType *Ty, uint32_t &TypeId,
862 bool CheckPointer, bool SeenPointer) {
863 if (!Ty || DIToIdMap.find(x: Ty) != DIToIdMap.end()) {
864 TypeId = DIToIdMap[Ty];
865
866 // To handle the case like the following:
867 // struct t;
868 // typedef struct t _t;
869 // struct s1 { _t *c; };
870 // int test1(struct s1 *arg) { ... }
871 //
872 // struct t { int a; int b; };
873 // struct s2 { _t c; }
874 // int test2(struct s2 *arg) { ... }
875 //
876 // During traversing test1() argument, "_t" is recorded
877 // in DIToIdMap and a forward declaration fixup is created
878 // for "struct t" to avoid pointee type traversal.
879 //
880 // During traversing test2() argument, even if we see "_t" is
881 // already defined, we should keep moving to eventually
882 // bring in types for "struct t". Otherwise, the "struct s2"
883 // definition won't be correct.
884 //
885 // In the above, we have following debuginfo:
886 // {ptr, struct_member} -> typedef -> struct
887 // and BTF type for 'typedef' is generated while 'struct' may
888 // be in FixUp. But let us generalize the above to handle
889 // {different types} -> [various derived types]+ -> another type.
890 // For example,
891 // {func_param, struct_member} -> const -> ptr -> volatile -> struct
892 // We will traverse const/ptr/volatile which already have corresponding
893 // BTF types and generate type for 'struct' which might be in Fixup
894 // state.
895 if (Ty && (!CheckPointer || !SeenPointer)) {
896 if (const auto *DTy = dyn_cast<DIDerivedType>(Val: Ty)) {
897 while (DTy) {
898 const DIType *BaseTy = DTy->getBaseType();
899 if (!BaseTy)
900 break;
901
902 if (DIToIdMap.find(x: BaseTy) != DIToIdMap.end()) {
903 DTy = dyn_cast<DIDerivedType>(Val: BaseTy);
904 } else {
905 if (CheckPointer && DTy->getTag() == dwarf::DW_TAG_pointer_type) {
906 SeenPointer = true;
907 if (IsForwardDeclCandidate(Base: BaseTy))
908 break;
909 }
910 uint32_t TmpTypeId;
911 visitTypeEntry(Ty: BaseTy, TypeId&: TmpTypeId, CheckPointer, SeenPointer);
912 break;
913 }
914 }
915 }
916 }
917
918 return;
919 }
920
921 if (const auto *BTy = dyn_cast<DIBasicType>(Val: Ty))
922 visitBasicType(BTy, TypeId);
923 else if (const auto *STy = dyn_cast<DISubroutineType>(Val: Ty))
924 visitSubroutineType(STy, ForSubprog: false, FuncArgNames: std::unordered_map<uint32_t, StringRef>(),
925 TypeId);
926 else if (const auto *CTy = dyn_cast<DICompositeType>(Val: Ty))
927 visitCompositeType(CTy, TypeId);
928 else if (const auto *DTy = dyn_cast<DIDerivedType>(Val: Ty))
929 visitDerivedType(DTy, TypeId, CheckPointer, SeenPointer);
930 else
931 llvm_unreachable("Unknown DIType");
932}
933
934void BTFDebug::visitTypeEntry(const DIType *Ty) {
935 uint32_t TypeId;
936 visitTypeEntry(Ty, TypeId, CheckPointer: false, SeenPointer: false);
937}
938
939void BTFDebug::visitMapDefType(const DIType *Ty, uint32_t &TypeId) {
940 if (!Ty || DIToIdMap.find(x: Ty) != DIToIdMap.end()) {
941 TypeId = DIToIdMap[Ty];
942 return;
943 }
944
945 // MapDef type may be a struct type or a non-pointer derived type
946 const DIType *OrigTy = Ty;
947 while (auto *DTy = dyn_cast<DIDerivedType>(Val: Ty)) {
948 auto Tag = DTy->getTag();
949 if (Tag != dwarf::DW_TAG_typedef && Tag != dwarf::DW_TAG_const_type &&
950 Tag != dwarf::DW_TAG_volatile_type &&
951 Tag != dwarf::DW_TAG_restrict_type)
952 break;
953 Ty = DTy->getBaseType();
954 }
955
956 const auto *CTy = dyn_cast<DICompositeType>(Val: Ty);
957 if (!CTy)
958 return;
959
960 auto Tag = CTy->getTag();
961 if (Tag != dwarf::DW_TAG_structure_type || CTy->isForwardDecl())
962 return;
963
964 // Visit all struct members to ensure pointee type is visited
965 const DINodeArray Elements = CTy->getElements();
966 for (const auto *Element : Elements) {
967 const auto *MemberType = cast<DIDerivedType>(Val: Element);
968 visitTypeEntry(Ty: MemberType->getBaseType());
969 }
970
971 // Visit this type, struct or a const/typedef/volatile/restrict type
972 visitTypeEntry(Ty: OrigTy, TypeId, CheckPointer: false, SeenPointer: false);
973}
974
975/// Read file contents from the actual file or from the source
976std::string BTFDebug::populateFileContent(const DISubprogram *SP) {
977 auto File = SP->getFile();
978 std::string FileName;
979
980 if (!File->getFilename().starts_with(Prefix: "/") && File->getDirectory().size())
981 FileName = File->getDirectory().str() + "/" + File->getFilename().str();
982 else
983 FileName = std::string(File->getFilename());
984
985 // No need to populate the contends if it has been populated!
986 if (FileContent.contains(Key: FileName))
987 return FileName;
988
989 std::vector<std::string> Content;
990 std::string Line;
991 Content.push_back(x: Line); // Line 0 for empty string
992
993 std::unique_ptr<MemoryBuffer> Buf;
994 auto Source = File->getSource();
995 if (Source)
996 Buf = MemoryBuffer::getMemBufferCopy(InputData: *Source);
997 else if (ErrorOr<std::unique_ptr<MemoryBuffer>> BufOrErr =
998 MemoryBuffer::getFile(Filename: FileName))
999 Buf = std::move(*BufOrErr);
1000 if (Buf)
1001 for (line_iterator I(*Buf, false), E; I != E; ++I)
1002 Content.push_back(x: std::string(*I));
1003
1004 FileContent[FileName] = Content;
1005 return FileName;
1006}
1007
1008void BTFDebug::constructLineInfo(const DISubprogram *SP, MCSymbol *Label,
1009 uint32_t Line, uint32_t Column) {
1010 std::string FileName = populateFileContent(SP);
1011 BTFLineInfo LineInfo;
1012
1013 LineInfo.Label = Label;
1014 LineInfo.FileNameOff = addString(S: FileName);
1015 // If file content is not available, let LineOff = 0.
1016 if (Line < FileContent[FileName].size())
1017 LineInfo.LineOff = addString(S: FileContent[FileName][Line]);
1018 else
1019 LineInfo.LineOff = 0;
1020 LineInfo.LineNum = Line;
1021 LineInfo.ColumnNum = Column;
1022 LineInfoTable[SecNameOff].push_back(x: LineInfo);
1023}
1024
1025void BTFDebug::emitCommonHeader() {
1026 OS.AddComment(T: "0x" + Twine::utohexstr(Val: BTF::MAGIC));
1027 OS.emitIntValue(Value: BTF::MAGIC, Size: 2);
1028 OS.emitInt8(Value: BTF::VERSION);
1029 OS.emitInt8(Value: 0);
1030}
1031
1032void BTFDebug::emitBTFSection() {
1033 // Do not emit section if no types and only "" string.
1034 if (!TypeEntries.size() && StringTable.getSize() == 1)
1035 return;
1036
1037 MCContext &Ctx = OS.getContext();
1038 MCSectionELF *Sec = Ctx.getELFSection(Section: ".BTF", Type: ELF::SHT_PROGBITS, Flags: 0);
1039 Sec->setAlignment(Align(4));
1040 OS.switchSection(Section: Sec);
1041
1042 // Emit header.
1043 emitCommonHeader();
1044 OS.emitInt32(Value: BTF::HeaderSize);
1045
1046 uint32_t TypeLen = 0, StrLen;
1047 for (const auto &TypeEntry : TypeEntries)
1048 TypeLen += TypeEntry->getSize();
1049 StrLen = StringTable.getSize();
1050
1051 OS.emitInt32(Value: 0);
1052 OS.emitInt32(Value: TypeLen);
1053 OS.emitInt32(Value: TypeLen);
1054 OS.emitInt32(Value: StrLen);
1055
1056 // Emit type table.
1057 for (const auto &TypeEntry : TypeEntries)
1058 TypeEntry->emitType(OS);
1059
1060 // Emit string table.
1061 uint32_t StringOffset = 0;
1062 for (const auto &S : StringTable.getTable()) {
1063 OS.AddComment(T: "string offset=" + std::to_string(val: StringOffset));
1064 OS.emitBytes(Data: S);
1065 OS.emitBytes(Data: StringRef("\0", 1));
1066 StringOffset += S.size() + 1;
1067 }
1068}
1069
1070void BTFDebug::emitBTFExtSection() {
1071 // Do not emit section if empty FuncInfoTable and LineInfoTable
1072 // and FieldRelocTable.
1073 if (!FuncInfoTable.size() && !LineInfoTable.size() &&
1074 !FieldRelocTable.size())
1075 return;
1076
1077 MCContext &Ctx = OS.getContext();
1078 MCSectionELF *Sec = Ctx.getELFSection(Section: ".BTF.ext", Type: ELF::SHT_PROGBITS, Flags: 0);
1079 Sec->setAlignment(Align(4));
1080 OS.switchSection(Section: Sec);
1081
1082 // Emit header.
1083 emitCommonHeader();
1084 OS.emitInt32(Value: BTF::ExtHeaderSize);
1085
1086 // Account for FuncInfo/LineInfo record size as well.
1087 uint32_t FuncLen = 4, LineLen = 4;
1088 // Do not account for optional FieldReloc.
1089 uint32_t FieldRelocLen = 0;
1090 for (const auto &FuncSec : FuncInfoTable) {
1091 FuncLen += BTF::SecFuncInfoSize;
1092 FuncLen += FuncSec.second.size() * BTF::BPFFuncInfoSize;
1093 }
1094 for (const auto &LineSec : LineInfoTable) {
1095 LineLen += BTF::SecLineInfoSize;
1096 LineLen += LineSec.second.size() * BTF::BPFLineInfoSize;
1097 }
1098 for (const auto &FieldRelocSec : FieldRelocTable) {
1099 FieldRelocLen += BTF::SecFieldRelocSize;
1100 FieldRelocLen += FieldRelocSec.second.size() * BTF::BPFFieldRelocSize;
1101 }
1102
1103 if (FieldRelocLen)
1104 FieldRelocLen += 4;
1105
1106 OS.emitInt32(Value: 0);
1107 OS.emitInt32(Value: FuncLen);
1108 OS.emitInt32(Value: FuncLen);
1109 OS.emitInt32(Value: LineLen);
1110 OS.emitInt32(Value: FuncLen + LineLen);
1111 OS.emitInt32(Value: FieldRelocLen);
1112
1113 // Emit func_info table.
1114 OS.AddComment(T: "FuncInfo");
1115 OS.emitInt32(Value: BTF::BPFFuncInfoSize);
1116 for (const auto &FuncSec : FuncInfoTable) {
1117 OS.AddComment(T: "FuncInfo section string offset=" +
1118 std::to_string(val: FuncSec.first));
1119 OS.emitInt32(Value: FuncSec.first);
1120 OS.emitInt32(Value: FuncSec.second.size());
1121 for (const auto &FuncInfo : FuncSec.second) {
1122 Asm->emitLabelReference(Label: FuncInfo.Label, Size: 4);
1123 OS.emitInt32(Value: FuncInfo.TypeId);
1124 }
1125 }
1126
1127 // Emit line_info table.
1128 OS.AddComment(T: "LineInfo");
1129 OS.emitInt32(Value: BTF::BPFLineInfoSize);
1130 for (const auto &LineSec : LineInfoTable) {
1131 OS.AddComment(T: "LineInfo section string offset=" +
1132 std::to_string(val: LineSec.first));
1133 OS.emitInt32(Value: LineSec.first);
1134 OS.emitInt32(Value: LineSec.second.size());
1135 for (const auto &LineInfo : LineSec.second) {
1136 Asm->emitLabelReference(Label: LineInfo.Label, Size: 4);
1137 OS.emitInt32(Value: LineInfo.FileNameOff);
1138 OS.emitInt32(Value: LineInfo.LineOff);
1139 OS.AddComment(T: "Line " + std::to_string(val: LineInfo.LineNum) + " Col " +
1140 std::to_string(val: LineInfo.ColumnNum));
1141 OS.emitInt32(Value: LineInfo.LineNum << 10 | LineInfo.ColumnNum);
1142 }
1143 }
1144
1145 // Emit field reloc table.
1146 if (FieldRelocLen) {
1147 OS.AddComment(T: "FieldReloc");
1148 OS.emitInt32(Value: BTF::BPFFieldRelocSize);
1149 for (const auto &FieldRelocSec : FieldRelocTable) {
1150 OS.AddComment(T: "Field reloc section string offset=" +
1151 std::to_string(val: FieldRelocSec.first));
1152 OS.emitInt32(Value: FieldRelocSec.first);
1153 OS.emitInt32(Value: FieldRelocSec.second.size());
1154 for (const auto &FieldRelocInfo : FieldRelocSec.second) {
1155 Asm->emitLabelReference(Label: FieldRelocInfo.Label, Size: 4);
1156 OS.emitInt32(Value: FieldRelocInfo.TypeID);
1157 OS.emitInt32(Value: FieldRelocInfo.OffsetNameOff);
1158 OS.emitInt32(Value: FieldRelocInfo.RelocKind);
1159 }
1160 }
1161 }
1162}
1163
1164void BTFDebug::beginFunctionImpl(const MachineFunction *MF) {
1165 auto *SP = MF->getFunction().getSubprogram();
1166 auto *Unit = SP->getUnit();
1167
1168 if (Unit->getEmissionKind() == DICompileUnit::NoDebug) {
1169 SkipInstruction = true;
1170 return;
1171 }
1172 SkipInstruction = false;
1173
1174 // Collect MapDef types. Map definition needs to collect
1175 // pointee types. Do it first. Otherwise, for the following
1176 // case:
1177 // struct m { ...};
1178 // struct t {
1179 // struct m *key;
1180 // };
1181 // foo(struct t *arg);
1182 //
1183 // struct mapdef {
1184 // ...
1185 // struct m *key;
1186 // ...
1187 // } __attribute__((section(".maps"))) hash_map;
1188 //
1189 // If subroutine foo is traversed first, a type chain
1190 // "ptr->struct m(fwd)" will be created and later on
1191 // when traversing mapdef, since "ptr->struct m" exists,
1192 // the traversal of "struct m" will be omitted.
1193 if (MapDefNotCollected) {
1194 processGlobals(ProcessingMapDef: true);
1195 MapDefNotCollected = false;
1196 }
1197
1198 // Collect all types locally referenced in this function.
1199 // Use RetainedNodes so we can collect all argument names
1200 // even if the argument is not used.
1201 std::unordered_map<uint32_t, StringRef> FuncArgNames;
1202 for (const DINode *DN : SP->getRetainedNodes()) {
1203 if (const auto *DV = dyn_cast<DILocalVariable>(Val: DN)) {
1204 // Collect function arguments for subprogram func type.
1205 uint32_t Arg = DV->getArg();
1206 if (Arg) {
1207 visitTypeEntry(Ty: DV->getType());
1208 FuncArgNames[Arg] = DV->getName();
1209 }
1210 }
1211 }
1212
1213 // Construct subprogram func proto type.
1214 uint32_t ProtoTypeId;
1215 visitSubroutineType(STy: SP->getType(), ForSubprog: true, FuncArgNames, TypeId&: ProtoTypeId);
1216
1217 // Construct subprogram func type
1218 uint8_t Scope = SP->isLocalToUnit() ? BTF::FUNC_STATIC : BTF::FUNC_GLOBAL;
1219 uint32_t FuncTypeId = processDISubprogram(SP, ProtoTypeId, Scope);
1220
1221 for (const auto &TypeEntry : TypeEntries)
1222 TypeEntry->completeType(BDebug&: *this);
1223
1224 // Construct funcinfo and the first lineinfo for the function.
1225 MCSymbol *FuncLabel = Asm->getFunctionBegin();
1226 BTFFuncInfo FuncInfo;
1227 FuncInfo.Label = FuncLabel;
1228 FuncInfo.TypeId = FuncTypeId;
1229 if (FuncLabel->isInSection()) {
1230 MCSection &Section = FuncLabel->getSection();
1231 const MCSectionELF *SectionELF = dyn_cast<MCSectionELF>(Val: &Section);
1232 assert(SectionELF && "Null section for Function Label");
1233 SecNameOff = addString(S: SectionELF->getName());
1234 } else {
1235 SecNameOff = addString(S: ".text");
1236 }
1237 FuncInfoTable[SecNameOff].push_back(x: FuncInfo);
1238}
1239
1240void BTFDebug::endFunctionImpl(const MachineFunction *MF) {
1241 SkipInstruction = false;
1242 LineInfoGenerated = false;
1243 SecNameOff = 0;
1244}
1245
1246/// On-demand populate types as requested from abstract member
1247/// accessing or preserve debuginfo type.
1248unsigned BTFDebug::populateType(const DIType *Ty) {
1249 unsigned Id;
1250 visitTypeEntry(Ty, TypeId&: Id, CheckPointer: false, SeenPointer: false);
1251 for (const auto &TypeEntry : TypeEntries)
1252 TypeEntry->completeType(BDebug&: *this);
1253 return Id;
1254}
1255
1256/// Generate a struct member field relocation.
1257void BTFDebug::generatePatchImmReloc(const MCSymbol *ORSym, uint32_t RootId,
1258 const GlobalVariable *GVar, bool IsAma) {
1259 BTFFieldReloc FieldReloc;
1260 FieldReloc.Label = ORSym;
1261 FieldReloc.TypeID = RootId;
1262
1263 StringRef AccessPattern = GVar->getName();
1264 size_t FirstDollar = AccessPattern.find_first_of(C: '$');
1265 if (IsAma) {
1266 size_t FirstColon = AccessPattern.find_first_of(C: ':');
1267 size_t SecondColon = AccessPattern.find_first_of(C: ':', From: FirstColon + 1);
1268 StringRef IndexPattern = AccessPattern.substr(Start: FirstDollar + 1);
1269 StringRef RelocKindStr = AccessPattern.substr(Start: FirstColon + 1,
1270 N: SecondColon - FirstColon);
1271 StringRef PatchImmStr = AccessPattern.substr(Start: SecondColon + 1,
1272 N: FirstDollar - SecondColon);
1273
1274 FieldReloc.OffsetNameOff = addString(S: IndexPattern);
1275 FieldReloc.RelocKind = std::stoull(str: std::string(RelocKindStr));
1276 PatchImms[GVar] = std::make_pair(x: std::stoll(str: std::string(PatchImmStr)),
1277 y&: FieldReloc.RelocKind);
1278 } else {
1279 StringRef RelocStr = AccessPattern.substr(Start: FirstDollar + 1);
1280 FieldReloc.OffsetNameOff = addString(S: "0");
1281 FieldReloc.RelocKind = std::stoull(str: std::string(RelocStr));
1282 PatchImms[GVar] = std::make_pair(x&: RootId, y&: FieldReloc.RelocKind);
1283 }
1284 FieldRelocTable[SecNameOff].push_back(x: FieldReloc);
1285}
1286
1287void BTFDebug::processGlobalValue(const MachineOperand &MO) {
1288 // check whether this is a candidate or not
1289 if (MO.isGlobal()) {
1290 const GlobalValue *GVal = MO.getGlobal();
1291 auto *GVar = dyn_cast<GlobalVariable>(Val: GVal);
1292 if (!GVar) {
1293 // Not a global variable. Maybe an extern function reference.
1294 processFuncPrototypes(dyn_cast<Function>(Val: GVal));
1295 return;
1296 }
1297
1298 if (!GVar->hasAttribute(Kind: BPFCoreSharedInfo::AmaAttr) &&
1299 !GVar->hasAttribute(Kind: BPFCoreSharedInfo::TypeIdAttr))
1300 return;
1301
1302 MCSymbol *ORSym = OS.getContext().createTempSymbol();
1303 OS.emitLabel(Symbol: ORSym);
1304
1305 MDNode *MDN = GVar->getMetadata(KindID: LLVMContext::MD_preserve_access_index);
1306 uint32_t RootId = populateType(Ty: dyn_cast<DIType>(Val: MDN));
1307 generatePatchImmReloc(ORSym, RootId, GVar,
1308 IsAma: GVar->hasAttribute(Kind: BPFCoreSharedInfo::AmaAttr));
1309 }
1310}
1311
1312void BTFDebug::beginInstruction(const MachineInstr *MI) {
1313 DebugHandlerBase::beginInstruction(MI);
1314
1315 if (SkipInstruction || MI->isMetaInstruction() ||
1316 MI->getFlag(Flag: MachineInstr::FrameSetup))
1317 return;
1318
1319 if (MI->isInlineAsm()) {
1320 // Count the number of register definitions to find the asm string.
1321 unsigned NumDefs = 0;
1322 while (true) {
1323 const MachineOperand &MO = MI->getOperand(i: NumDefs);
1324 if (MO.isReg() && MO.isDef()) {
1325 ++NumDefs;
1326 continue;
1327 }
1328 // Skip this inline asm instruction if the asmstr is empty.
1329 const char *AsmStr = MO.getSymbolName();
1330 if (AsmStr[0] == 0)
1331 return;
1332 break;
1333 }
1334 }
1335
1336 if (MI->getOpcode() == BPF::LD_imm64) {
1337 // If the insn is "r2 = LD_imm64 @<an AmaAttr global>",
1338 // add this insn into the .BTF.ext FieldReloc subsection.
1339 // Relocation looks like:
1340 // . SecName:
1341 // . InstOffset
1342 // . TypeID
1343 // . OffSetNameOff
1344 // . RelocType
1345 // Later, the insn is replaced with "r2 = <offset>"
1346 // where "<offset>" equals to the offset based on current
1347 // type definitions.
1348 //
1349 // If the insn is "r2 = LD_imm64 @<an TypeIdAttr global>",
1350 // The LD_imm64 result will be replaced with a btf type id.
1351 processGlobalValue(MO: MI->getOperand(i: 1));
1352 } else if (MI->getOpcode() == BPF::CORE_LD64 ||
1353 MI->getOpcode() == BPF::CORE_LD32 ||
1354 MI->getOpcode() == BPF::CORE_ST ||
1355 MI->getOpcode() == BPF::CORE_SHIFT) {
1356 // relocation insn is a load, store or shift insn.
1357 processGlobalValue(MO: MI->getOperand(i: 3));
1358 } else if (MI->getOpcode() == BPF::JAL) {
1359 // check extern function references
1360 const MachineOperand &MO = MI->getOperand(i: 0);
1361 if (MO.isGlobal()) {
1362 processFuncPrototypes(dyn_cast<Function>(Val: MO.getGlobal()));
1363 }
1364 }
1365
1366 if (!CurMI) // no debug info
1367 return;
1368
1369 // Skip this instruction if no DebugLoc or the DebugLoc
1370 // is the same as the previous instruction.
1371 const DebugLoc &DL = MI->getDebugLoc();
1372 if (!DL || PrevInstLoc == DL) {
1373 // This instruction will be skipped, no LineInfo has
1374 // been generated, construct one based on function signature.
1375 if (LineInfoGenerated == false) {
1376 auto *S = MI->getMF()->getFunction().getSubprogram();
1377 if (!S)
1378 return;
1379 MCSymbol *FuncLabel = Asm->getFunctionBegin();
1380 constructLineInfo(SP: S, Label: FuncLabel, Line: S->getLine(), Column: 0);
1381 LineInfoGenerated = true;
1382 }
1383
1384 return;
1385 }
1386
1387 // Create a temporary label to remember the insn for lineinfo.
1388 MCSymbol *LineSym = OS.getContext().createTempSymbol();
1389 OS.emitLabel(Symbol: LineSym);
1390
1391 // Construct the lineinfo.
1392 auto SP = DL->getScope()->getSubprogram();
1393 constructLineInfo(SP, Label: LineSym, Line: DL.getLine(), Column: DL.getCol());
1394
1395 LineInfoGenerated = true;
1396 PrevInstLoc = DL;
1397}
1398
1399void BTFDebug::processGlobals(bool ProcessingMapDef) {
1400 // Collect all types referenced by globals.
1401 const Module *M = MMI->getModule();
1402 for (const GlobalVariable &Global : M->globals()) {
1403 // Decide the section name.
1404 StringRef SecName;
1405 std::optional<SectionKind> GVKind;
1406
1407 if (!Global.isDeclarationForLinker())
1408 GVKind = TargetLoweringObjectFile::getKindForGlobal(GO: &Global, TM: Asm->TM);
1409
1410 if (Global.isDeclarationForLinker())
1411 SecName = Global.hasSection() ? Global.getSection() : "";
1412 else if (GVKind->isCommon())
1413 SecName = ".bss";
1414 else {
1415 TargetLoweringObjectFile *TLOF = Asm->TM.getObjFileLowering();
1416 MCSection *Sec = TLOF->SectionForGlobal(GO: &Global, TM: Asm->TM);
1417 SecName = Sec->getName();
1418 }
1419
1420 if (ProcessingMapDef != SecName.starts_with(Prefix: ".maps"))
1421 continue;
1422
1423 // Create a .rodata datasec if the global variable is an initialized
1424 // constant with private linkage and if it won't be in .rodata.str<#>
1425 // and .rodata.cst<#> sections.
1426 if (SecName == ".rodata" && Global.hasPrivateLinkage() &&
1427 DataSecEntries.find(x: std::string(SecName)) == DataSecEntries.end()) {
1428 // skip .rodata.str<#> and .rodata.cst<#> sections
1429 if (!GVKind->isMergeableCString() && !GVKind->isMergeableConst()) {
1430 DataSecEntries[std::string(SecName)] =
1431 std::make_unique<BTFKindDataSec>(args&: Asm, args: std::string(SecName));
1432 }
1433 }
1434
1435 SmallVector<DIGlobalVariableExpression *, 1> GVs;
1436 Global.getDebugInfo(GVs);
1437
1438 // No type information, mostly internal, skip it.
1439 if (GVs.size() == 0)
1440 continue;
1441
1442 uint32_t GVTypeId = 0;
1443 DIGlobalVariable *DIGlobal = nullptr;
1444 for (auto *GVE : GVs) {
1445 DIGlobal = GVE->getVariable();
1446 if (SecName.starts_with(Prefix: ".maps"))
1447 visitMapDefType(Ty: DIGlobal->getType(), TypeId&: GVTypeId);
1448 else
1449 visitTypeEntry(Ty: DIGlobal->getType(), TypeId&: GVTypeId, CheckPointer: false, SeenPointer: false);
1450 break;
1451 }
1452
1453 // Only support the following globals:
1454 // . static variables
1455 // . non-static weak or non-weak global variables
1456 // . weak or non-weak extern global variables
1457 // Whether DataSec is readonly or not can be found from corresponding ELF
1458 // section flags. Whether a BTF_KIND_VAR is a weak symbol or not
1459 // can be found from the corresponding ELF symbol table.
1460 auto Linkage = Global.getLinkage();
1461 if (Linkage != GlobalValue::InternalLinkage &&
1462 Linkage != GlobalValue::ExternalLinkage &&
1463 Linkage != GlobalValue::WeakAnyLinkage &&
1464 Linkage != GlobalValue::WeakODRLinkage &&
1465 Linkage != GlobalValue::ExternalWeakLinkage)
1466 continue;
1467
1468 uint32_t GVarInfo;
1469 if (Linkage == GlobalValue::InternalLinkage) {
1470 GVarInfo = BTF::VAR_STATIC;
1471 } else if (Global.hasInitializer()) {
1472 GVarInfo = BTF::VAR_GLOBAL_ALLOCATED;
1473 } else {
1474 GVarInfo = BTF::VAR_GLOBAL_EXTERNAL;
1475 }
1476
1477 auto VarEntry =
1478 std::make_unique<BTFKindVar>(args: Global.getName(), args&: GVTypeId, args&: GVarInfo);
1479 uint32_t VarId = addType(TypeEntry: std::move(VarEntry));
1480
1481 processDeclAnnotations(Annotations: DIGlobal->getAnnotations(), BaseTypeId: VarId, ComponentIdx: -1);
1482
1483 // An empty SecName means an extern variable without section attribute.
1484 if (SecName.empty())
1485 continue;
1486
1487 // Find or create a DataSec
1488 if (DataSecEntries.find(x: std::string(SecName)) == DataSecEntries.end()) {
1489 DataSecEntries[std::string(SecName)] =
1490 std::make_unique<BTFKindDataSec>(args&: Asm, args: std::string(SecName));
1491 }
1492
1493 // Calculate symbol size
1494 const DataLayout &DL = Global.getParent()->getDataLayout();
1495 uint32_t Size = DL.getTypeAllocSize(Ty: Global.getValueType());
1496
1497 DataSecEntries[std::string(SecName)]->addDataSecEntry(Id: VarId,
1498 Sym: Asm->getSymbol(GV: &Global), Size);
1499 }
1500}
1501
1502/// Emit proper patchable instructions.
1503bool BTFDebug::InstLower(const MachineInstr *MI, MCInst &OutMI) {
1504 if (MI->getOpcode() == BPF::LD_imm64) {
1505 const MachineOperand &MO = MI->getOperand(i: 1);
1506 if (MO.isGlobal()) {
1507 const GlobalValue *GVal = MO.getGlobal();
1508 auto *GVar = dyn_cast<GlobalVariable>(Val: GVal);
1509 if (GVar) {
1510 // Emit "mov ri, <imm>"
1511 int64_t Imm;
1512 uint32_t Reloc;
1513 if (GVar->hasAttribute(Kind: BPFCoreSharedInfo::AmaAttr) ||
1514 GVar->hasAttribute(Kind: BPFCoreSharedInfo::TypeIdAttr)) {
1515 Imm = PatchImms[GVar].first;
1516 Reloc = PatchImms[GVar].second;
1517 } else {
1518 return false;
1519 }
1520
1521 if (Reloc == BTF::ENUM_VALUE_EXISTENCE || Reloc == BTF::ENUM_VALUE ||
1522 Reloc == BTF::BTF_TYPE_ID_LOCAL || Reloc == BTF::BTF_TYPE_ID_REMOTE)
1523 OutMI.setOpcode(BPF::LD_imm64);
1524 else
1525 OutMI.setOpcode(BPF::MOV_ri);
1526 OutMI.addOperand(Op: MCOperand::createReg(Reg: MI->getOperand(i: 0).getReg()));
1527 OutMI.addOperand(Op: MCOperand::createImm(Val: Imm));
1528 return true;
1529 }
1530 }
1531 } else if (MI->getOpcode() == BPF::CORE_LD64 ||
1532 MI->getOpcode() == BPF::CORE_LD32 ||
1533 MI->getOpcode() == BPF::CORE_ST ||
1534 MI->getOpcode() == BPF::CORE_SHIFT) {
1535 const MachineOperand &MO = MI->getOperand(i: 3);
1536 if (MO.isGlobal()) {
1537 const GlobalValue *GVal = MO.getGlobal();
1538 auto *GVar = dyn_cast<GlobalVariable>(Val: GVal);
1539 if (GVar && GVar->hasAttribute(Kind: BPFCoreSharedInfo::AmaAttr)) {
1540 uint32_t Imm = PatchImms[GVar].first;
1541 OutMI.setOpcode(MI->getOperand(i: 1).getImm());
1542 if (MI->getOperand(i: 0).isImm())
1543 OutMI.addOperand(Op: MCOperand::createImm(Val: MI->getOperand(i: 0).getImm()));
1544 else
1545 OutMI.addOperand(Op: MCOperand::createReg(Reg: MI->getOperand(i: 0).getReg()));
1546 OutMI.addOperand(Op: MCOperand::createReg(Reg: MI->getOperand(i: 2).getReg()));
1547 OutMI.addOperand(Op: MCOperand::createImm(Val: Imm));
1548 return true;
1549 }
1550 }
1551 }
1552 return false;
1553}
1554
1555void BTFDebug::processFuncPrototypes(const Function *F) {
1556 if (!F)
1557 return;
1558
1559 const DISubprogram *SP = F->getSubprogram();
1560 if (!SP || SP->isDefinition())
1561 return;
1562
1563 // Do not emit again if already emitted.
1564 if (!ProtoFunctions.insert(x: F).second)
1565 return;
1566
1567 uint32_t ProtoTypeId;
1568 const std::unordered_map<uint32_t, StringRef> FuncArgNames;
1569 visitSubroutineType(STy: SP->getType(), ForSubprog: false, FuncArgNames, TypeId&: ProtoTypeId);
1570 uint32_t FuncId = processDISubprogram(SP, ProtoTypeId, Scope: BTF::FUNC_EXTERN);
1571
1572 if (F->hasSection()) {
1573 StringRef SecName = F->getSection();
1574
1575 if (DataSecEntries.find(x: std::string(SecName)) == DataSecEntries.end()) {
1576 DataSecEntries[std::string(SecName)] =
1577 std::make_unique<BTFKindDataSec>(args&: Asm, args: std::string(SecName));
1578 }
1579
1580 // We really don't know func size, set it to 0.
1581 DataSecEntries[std::string(SecName)]->addDataSecEntry(Id: FuncId,
1582 Sym: Asm->getSymbol(GV: F), Size: 0);
1583 }
1584}
1585
1586void BTFDebug::endModule() {
1587 // Collect MapDef globals if not collected yet.
1588 if (MapDefNotCollected) {
1589 processGlobals(ProcessingMapDef: true);
1590 MapDefNotCollected = false;
1591 }
1592
1593 // Collect global types/variables except MapDef globals.
1594 processGlobals(ProcessingMapDef: false);
1595
1596 for (auto &DataSec : DataSecEntries)
1597 addType(TypeEntry: std::move(DataSec.second));
1598
1599 // Fixups
1600 for (auto &Fixup : FixupDerivedTypes) {
1601 const DICompositeType *CTy = Fixup.first;
1602 StringRef TypeName = CTy->getName();
1603 bool IsUnion = CTy->getTag() == dwarf::DW_TAG_union_type;
1604
1605 // Search through struct types
1606 uint32_t StructTypeId = 0;
1607 for (const auto &StructType : StructTypes) {
1608 if (StructType->getName() == TypeName) {
1609 StructTypeId = StructType->getId();
1610 break;
1611 }
1612 }
1613
1614 if (StructTypeId == 0) {
1615 auto FwdTypeEntry = std::make_unique<BTFTypeFwd>(args&: TypeName, args&: IsUnion);
1616 StructTypeId = addType(TypeEntry: std::move(FwdTypeEntry));
1617 }
1618
1619 for (auto &TypeInfo : Fixup.second) {
1620 const DIDerivedType *DTy = TypeInfo.first;
1621 BTFTypeDerived *BDType = TypeInfo.second;
1622
1623 int TmpTypeId = genBTFTypeTags(DTy, BaseTypeId: StructTypeId);
1624 if (TmpTypeId >= 0)
1625 BDType->setPointeeType(TmpTypeId);
1626 else
1627 BDType->setPointeeType(StructTypeId);
1628 }
1629 }
1630
1631 // Complete BTF type cross refereences.
1632 for (const auto &TypeEntry : TypeEntries)
1633 TypeEntry->completeType(BDebug&: *this);
1634
1635 // Emit BTF sections.
1636 emitBTFSection();
1637 emitBTFExtSection();
1638}
1639

source code of llvm/lib/Target/BPF/BTFDebug.cpp