1 //===- BTFDebug.cpp - BTF Generator ---------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file contains support for writing BTF debug info.
11 //===----------------------------------------------------------------------===//
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/MC/MCContext.h"
21 #include "llvm/MC/MCObjectFileInfo.h"
22 #include "llvm/MC/MCSectionELF.h"
23 #include "llvm/MC/MCStreamer.h"
24 #include "llvm/Support/LineIterator.h"
28 static const char *BTFKindStr[] = {
29 #define HANDLE_BTF_KIND(ID, NAME) "BTF_KIND_" #NAME,
33 /// Emit a BTF common type.
34 void BTFTypeBase::emitType(MCStreamer &OS) {
35 OS.AddComment(std::string(BTFKindStr[Kind]) + "(id = " + std::to_string(Id) +
37 OS.emitInt32(BTFType.NameOff);
38 OS.AddComment("0x" + Twine::utohexstr(BTFType.Info));
39 OS.emitInt32(BTFType.Info);
40 OS.emitInt32(BTFType.Size);
43 BTFTypeDerived::BTFTypeDerived(const DIDerivedType *DTy, unsigned Tag,
45 : DTy(DTy), NeedsFixup(NeedsFixup) {
47 case dwarf::DW_TAG_pointer_type:
48 Kind = BTF::BTF_KIND_PTR;
50 case dwarf::DW_TAG_const_type:
51 Kind = BTF::BTF_KIND_CONST;
53 case dwarf::DW_TAG_volatile_type:
54 Kind = BTF::BTF_KIND_VOLATILE;
56 case dwarf::DW_TAG_typedef:
57 Kind = BTF::BTF_KIND_TYPEDEF;
59 case dwarf::DW_TAG_restrict_type:
60 Kind = BTF::BTF_KIND_RESTRICT;
63 llvm_unreachable("Unknown DIDerivedType Tag");
65 BTFType.Info = Kind << 24;
68 void BTFTypeDerived::completeType(BTFDebug &BDebug) {
73 BTFType.NameOff = BDebug.addString(DTy->getName());
78 // The base type for PTR/CONST/VOLATILE could be void.
79 const DIType *ResolvedType = DTy->getBaseType();
81 assert((Kind == BTF::BTF_KIND_PTR || Kind == BTF::BTF_KIND_CONST ||
82 Kind == BTF::BTF_KIND_VOLATILE) &&
83 "Invalid null basetype");
86 BTFType.Type = BDebug.getTypeId(ResolvedType);
90 void BTFTypeDerived::emitType(MCStreamer &OS) { BTFTypeBase::emitType(OS); }
92 void BTFTypeDerived::setPointeeType(uint32_t PointeeType) {
93 BTFType.Type = PointeeType;
96 /// Represent a struct/union forward declaration.
97 BTFTypeFwd::BTFTypeFwd(StringRef Name, bool IsUnion) : Name(Name) {
98 Kind = BTF::BTF_KIND_FWD;
99 BTFType.Info = IsUnion << 31 | Kind << 24;
103 void BTFTypeFwd::completeType(BTFDebug &BDebug) {
108 BTFType.NameOff = BDebug.addString(Name);
111 void BTFTypeFwd::emitType(MCStreamer &OS) { BTFTypeBase::emitType(OS); }
113 BTFTypeInt::BTFTypeInt(uint32_t Encoding, uint32_t SizeInBits,
114 uint32_t OffsetInBits, StringRef TypeName)
116 // Translate IR int encoding to BTF int encoding.
119 case dwarf::DW_ATE_boolean:
120 BTFEncoding = BTF::INT_BOOL;
122 case dwarf::DW_ATE_signed:
123 case dwarf::DW_ATE_signed_char:
124 BTFEncoding = BTF::INT_SIGNED;
126 case dwarf::DW_ATE_unsigned:
127 case dwarf::DW_ATE_unsigned_char:
131 llvm_unreachable("Unknown BTFTypeInt Encoding");
134 Kind = BTF::BTF_KIND_INT;
135 BTFType.Info = Kind << 24;
136 BTFType.Size = roundupToBytes(SizeInBits);
137 IntVal = (BTFEncoding << 24) | OffsetInBits << 16 | SizeInBits;
140 void BTFTypeInt::completeType(BTFDebug &BDebug) {
145 BTFType.NameOff = BDebug.addString(Name);
148 void BTFTypeInt::emitType(MCStreamer &OS) {
149 BTFTypeBase::emitType(OS);
150 OS.AddComment("0x" + Twine::utohexstr(IntVal));
151 OS.emitInt32(IntVal);
154 BTFTypeEnum::BTFTypeEnum(const DICompositeType *ETy, uint32_t VLen) : ETy(ETy) {
155 Kind = BTF::BTF_KIND_ENUM;
156 BTFType.Info = Kind << 24 | VLen;
157 BTFType.Size = roundupToBytes(ETy->getSizeInBits());
160 void BTFTypeEnum::completeType(BTFDebug &BDebug) {
165 BTFType.NameOff = BDebug.addString(ETy->getName());
167 DINodeArray Elements = ETy->getElements();
168 for (const auto Element : Elements) {
169 const auto *Enum = cast<DIEnumerator>(Element);
171 struct BTF::BTFEnum BTFEnum;
172 BTFEnum.NameOff = BDebug.addString(Enum->getName());
173 // BTF enum value is 32bit, enforce it.
175 if (Enum->isUnsigned())
176 Value = static_cast<uint32_t>(Enum->getValue().getZExtValue());
178 Value = static_cast<uint32_t>(Enum->getValue().getSExtValue());
180 EnumValues.push_back(BTFEnum);
184 void BTFTypeEnum::emitType(MCStreamer &OS) {
185 BTFTypeBase::emitType(OS);
186 for (const auto &Enum : EnumValues) {
187 OS.emitInt32(Enum.NameOff);
188 OS.emitInt32(Enum.Val);
192 BTFTypeArray::BTFTypeArray(uint32_t ElemTypeId, uint32_t NumElems) {
193 Kind = BTF::BTF_KIND_ARRAY;
195 BTFType.Info = Kind << 24;
198 ArrayInfo.ElemType = ElemTypeId;
199 ArrayInfo.Nelems = NumElems;
202 /// Represent a BTF array.
203 void BTFTypeArray::completeType(BTFDebug &BDebug) {
208 // The IR does not really have a type for the index.
209 // A special type for array index should have been
210 // created during initial type traversal. Just
211 // retrieve that type id.
212 ArrayInfo.IndexType = BDebug.getArrayIndexTypeId();
215 void BTFTypeArray::emitType(MCStreamer &OS) {
216 BTFTypeBase::emitType(OS);
217 OS.emitInt32(ArrayInfo.ElemType);
218 OS.emitInt32(ArrayInfo.IndexType);
219 OS.emitInt32(ArrayInfo.Nelems);
222 /// Represent either a struct or a union.
223 BTFTypeStruct::BTFTypeStruct(const DICompositeType *STy, bool IsStruct,
224 bool HasBitField, uint32_t Vlen)
225 : STy(STy), HasBitField(HasBitField) {
226 Kind = IsStruct ? BTF::BTF_KIND_STRUCT : BTF::BTF_KIND_UNION;
227 BTFType.Size = roundupToBytes(STy->getSizeInBits());
228 BTFType.Info = (HasBitField << 31) | (Kind << 24) | Vlen;
231 void BTFTypeStruct::completeType(BTFDebug &BDebug) {
236 BTFType.NameOff = BDebug.addString(STy->getName());
238 // Add struct/union members.
239 const DINodeArray Elements = STy->getElements();
240 for (const auto *Element : Elements) {
241 struct BTF::BTFMember BTFMember;
242 const auto *DDTy = cast<DIDerivedType>(Element);
244 BTFMember.NameOff = BDebug.addString(DDTy->getName());
246 uint8_t BitFieldSize = DDTy->isBitField() ? DDTy->getSizeInBits() : 0;
247 BTFMember.Offset = BitFieldSize << 24 | DDTy->getOffsetInBits();
249 BTFMember.Offset = DDTy->getOffsetInBits();
251 const auto *BaseTy = DDTy->getBaseType();
252 BTFMember.Type = BDebug.getTypeId(BaseTy);
253 Members.push_back(BTFMember);
257 void BTFTypeStruct::emitType(MCStreamer &OS) {
258 BTFTypeBase::emitType(OS);
259 for (const auto &Member : Members) {
260 OS.emitInt32(Member.NameOff);
261 OS.emitInt32(Member.Type);
262 OS.AddComment("0x" + Twine::utohexstr(Member.Offset));
263 OS.emitInt32(Member.Offset);
267 std::string BTFTypeStruct::getName() { return std::string(STy->getName()); }
269 /// The Func kind represents both subprogram and pointee of function
270 /// pointers. If the FuncName is empty, it represents a pointee of function
271 /// pointer. Otherwise, it represents a subprogram. The func arg names
272 /// are empty for pointee of function pointer case, and are valid names
274 BTFTypeFuncProto::BTFTypeFuncProto(
275 const DISubroutineType *STy, uint32_t VLen,
276 const std::unordered_map<uint32_t, StringRef> &FuncArgNames)
277 : STy(STy), FuncArgNames(FuncArgNames) {
278 Kind = BTF::BTF_KIND_FUNC_PROTO;
279 BTFType.Info = (Kind << 24) | VLen;
282 void BTFTypeFuncProto::completeType(BTFDebug &BDebug) {
287 DITypeRefArray Elements = STy->getTypeArray();
288 auto RetType = Elements[0];
289 BTFType.Type = RetType ? BDebug.getTypeId(RetType) : 0;
292 // For null parameter which is typically the last one
293 // to represent the vararg, encode the NameOff/Type to be 0.
294 for (unsigned I = 1, N = Elements.size(); I < N; ++I) {
295 struct BTF::BTFParam Param;
296 auto Element = Elements[I];
298 Param.NameOff = BDebug.addString(FuncArgNames[I]);
299 Param.Type = BDebug.getTypeId(Element);
304 Parameters.push_back(Param);
308 void BTFTypeFuncProto::emitType(MCStreamer &OS) {
309 BTFTypeBase::emitType(OS);
310 for (const auto &Param : Parameters) {
311 OS.emitInt32(Param.NameOff);
312 OS.emitInt32(Param.Type);
316 BTFTypeFunc::BTFTypeFunc(StringRef FuncName, uint32_t ProtoTypeId,
319 Kind = BTF::BTF_KIND_FUNC;
320 BTFType.Info = (Kind << 24) | Scope;
321 BTFType.Type = ProtoTypeId;
324 void BTFTypeFunc::completeType(BTFDebug &BDebug) {
329 BTFType.NameOff = BDebug.addString(Name);
332 void BTFTypeFunc::emitType(MCStreamer &OS) { BTFTypeBase::emitType(OS); }
334 BTFKindVar::BTFKindVar(StringRef VarName, uint32_t TypeId, uint32_t VarInfo)
336 Kind = BTF::BTF_KIND_VAR;
337 BTFType.Info = Kind << 24;
338 BTFType.Type = TypeId;
342 void BTFKindVar::completeType(BTFDebug &BDebug) {
343 BTFType.NameOff = BDebug.addString(Name);
346 void BTFKindVar::emitType(MCStreamer &OS) {
347 BTFTypeBase::emitType(OS);
351 BTFKindDataSec::BTFKindDataSec(AsmPrinter *AsmPrt, std::string SecName)
352 : Asm(AsmPrt), Name(SecName) {
353 Kind = BTF::BTF_KIND_DATASEC;
354 BTFType.Info = Kind << 24;
358 void BTFKindDataSec::completeType(BTFDebug &BDebug) {
359 BTFType.NameOff = BDebug.addString(Name);
360 BTFType.Info |= Vars.size();
363 void BTFKindDataSec::emitType(MCStreamer &OS) {
364 BTFTypeBase::emitType(OS);
366 for (const auto &V : Vars) {
367 OS.emitInt32(std::get<0>(V));
368 Asm->emitLabelReference(std::get<1>(V), 4);
369 OS.emitInt32(std::get<2>(V));
373 uint32_t BTFStringTable::addString(StringRef S) {
374 // Check whether the string already exists.
375 for (auto &OffsetM : OffsetToIdMap) {
376 if (Table[OffsetM.second] == S)
377 return OffsetM.first;
379 // Not find, add to the string table.
380 uint32_t Offset = Size;
381 OffsetToIdMap[Offset] = Table.size();
382 Table.push_back(std::string(S));
383 Size += S.size() + 1;
387 BTFDebug::BTFDebug(AsmPrinter *AP)
388 : DebugHandlerBase(AP), OS(*Asm->OutStreamer), SkipInstruction(false),
389 LineInfoGenerated(false), SecNameOff(0), ArrayIndexTypeId(0),
390 MapDefNotCollected(true) {
394 uint32_t BTFDebug::addType(std::unique_ptr<BTFTypeBase> TypeEntry,
396 TypeEntry->setId(TypeEntries.size() + 1);
397 uint32_t Id = TypeEntry->getId();
399 TypeEntries.push_back(std::move(TypeEntry));
403 uint32_t BTFDebug::addType(std::unique_ptr<BTFTypeBase> TypeEntry) {
404 TypeEntry->setId(TypeEntries.size() + 1);
405 uint32_t Id = TypeEntry->getId();
406 TypeEntries.push_back(std::move(TypeEntry));
410 void BTFDebug::visitBasicType(const DIBasicType *BTy, uint32_t &TypeId) {
411 // Only int types are supported in BTF.
412 uint32_t Encoding = BTy->getEncoding();
413 if (Encoding != dwarf::DW_ATE_boolean && Encoding != dwarf::DW_ATE_signed &&
414 Encoding != dwarf::DW_ATE_signed_char &&
415 Encoding != dwarf::DW_ATE_unsigned &&
416 Encoding != dwarf::DW_ATE_unsigned_char)
419 // Create a BTF type instance for this DIBasicType and put it into
420 // DIToIdMap for cross-type reference check.
421 auto TypeEntry = std::make_unique<BTFTypeInt>(
422 Encoding, BTy->getSizeInBits(), BTy->getOffsetInBits(), BTy->getName());
423 TypeId = addType(std::move(TypeEntry), BTy);
426 /// Handle subprogram or subroutine types.
427 void BTFDebug::visitSubroutineType(
428 const DISubroutineType *STy, bool ForSubprog,
429 const std::unordered_map<uint32_t, StringRef> &FuncArgNames,
431 DITypeRefArray Elements = STy->getTypeArray();
432 uint32_t VLen = Elements.size() - 1;
433 if (VLen > BTF::MAX_VLEN)
436 // Subprogram has a valid non-zero-length name, and the pointee of
437 // a function pointer has an empty name. The subprogram type will
438 // not be added to DIToIdMap as it should not be referenced by
440 auto TypeEntry = std::make_unique<BTFTypeFuncProto>(STy, VLen, FuncArgNames);
442 TypeId = addType(std::move(TypeEntry)); // For subprogram
444 TypeId = addType(std::move(TypeEntry), STy); // For func ptr
446 // Visit return type and func arg types.
447 for (const auto Element : Elements) {
448 visitTypeEntry(Element);
452 /// Handle structure/union types.
453 void BTFDebug::visitStructType(const DICompositeType *CTy, bool IsStruct,
455 const DINodeArray Elements = CTy->getElements();
456 uint32_t VLen = Elements.size();
457 if (VLen > BTF::MAX_VLEN)
460 // Check whether we have any bitfield members or not
461 bool HasBitField = false;
462 for (const auto *Element : Elements) {
463 auto E = cast<DIDerivedType>(Element);
464 if (E->isBitField()) {
471 std::make_unique<BTFTypeStruct>(CTy, IsStruct, HasBitField, VLen);
472 StructTypes.push_back(TypeEntry.get());
473 TypeId = addType(std::move(TypeEntry), CTy);
475 // Visit all struct members.
476 for (const auto *Element : Elements)
477 visitTypeEntry(cast<DIDerivedType>(Element));
480 void BTFDebug::visitArrayType(const DICompositeType *CTy, uint32_t &TypeId) {
481 // Visit array element type.
483 const DIType *ElemType = CTy->getBaseType();
484 visitTypeEntry(ElemType, ElemTypeId, false, false);
486 // Visit array dimensions.
487 DINodeArray Elements = CTy->getElements();
488 for (int I = Elements.size() - 1; I >= 0; --I) {
489 if (auto *Element = dyn_cast_or_null<DINode>(Elements[I]))
490 if (Element->getTag() == dwarf::DW_TAG_subrange_type) {
491 const DISubrange *SR = cast<DISubrange>(Element);
492 auto *CI = SR->getCount().dyn_cast<ConstantInt *>();
493 int64_t Count = CI->getSExtValue();
495 // For struct s { int b; char c[]; }, the c[] will be represented
496 // as an array with Count = -1.
498 std::make_unique<BTFTypeArray>(ElemTypeId,
499 Count >= 0 ? Count : 0);
501 ElemTypeId = addType(std::move(TypeEntry), CTy);
503 ElemTypeId = addType(std::move(TypeEntry));
507 // The array TypeId is the type id of the outermost dimension.
510 // The IR does not have a type for array index while BTF wants one.
511 // So create an array index type if there is none.
512 if (!ArrayIndexTypeId) {
513 auto TypeEntry = std::make_unique<BTFTypeInt>(dwarf::DW_ATE_unsigned, 32,
514 0, "__ARRAY_SIZE_TYPE__");
515 ArrayIndexTypeId = addType(std::move(TypeEntry));
519 void BTFDebug::visitEnumType(const DICompositeType *CTy, uint32_t &TypeId) {
520 DINodeArray Elements = CTy->getElements();
521 uint32_t VLen = Elements.size();
522 if (VLen > BTF::MAX_VLEN)
525 auto TypeEntry = std::make_unique<BTFTypeEnum>(CTy, VLen);
526 TypeId = addType(std::move(TypeEntry), CTy);
527 // No need to visit base type as BTF does not encode it.
530 /// Handle structure/union forward declarations.
531 void BTFDebug::visitFwdDeclType(const DICompositeType *CTy, bool IsUnion,
533 auto TypeEntry = std::make_unique<BTFTypeFwd>(CTy->getName(), IsUnion);
534 TypeId = addType(std::move(TypeEntry), CTy);
537 /// Handle structure, union, array and enumeration types.
538 void BTFDebug::visitCompositeType(const DICompositeType *CTy,
540 auto Tag = CTy->getTag();
541 if (Tag == dwarf::DW_TAG_structure_type || Tag == dwarf::DW_TAG_union_type) {
542 // Handle forward declaration differently as it does not have members.
543 if (CTy->isForwardDecl())
544 visitFwdDeclType(CTy, Tag == dwarf::DW_TAG_union_type, TypeId);
546 visitStructType(CTy, Tag == dwarf::DW_TAG_structure_type, TypeId);
547 } else if (Tag == dwarf::DW_TAG_array_type)
548 visitArrayType(CTy, TypeId);
549 else if (Tag == dwarf::DW_TAG_enumeration_type)
550 visitEnumType(CTy, TypeId);
553 /// Handle pointer, typedef, const, volatile, restrict and member types.
554 void BTFDebug::visitDerivedType(const DIDerivedType *DTy, uint32_t &TypeId,
555 bool CheckPointer, bool SeenPointer) {
556 unsigned Tag = DTy->getTag();
558 /// Try to avoid chasing pointees, esp. structure pointees which may
559 /// unnecessary bring in a lot of types.
560 if (CheckPointer && !SeenPointer) {
561 SeenPointer = Tag == dwarf::DW_TAG_pointer_type;
564 if (CheckPointer && SeenPointer) {
565 const DIType *Base = DTy->getBaseType();
567 if (const auto *CTy = dyn_cast<DICompositeType>(Base)) {
568 auto CTag = CTy->getTag();
569 if ((CTag == dwarf::DW_TAG_structure_type ||
570 CTag == dwarf::DW_TAG_union_type) &&
571 !CTy->getName().empty() && !CTy->isForwardDecl()) {
572 /// Find a candidate, generate a fixup. Later on the struct/union
573 /// pointee type will be replaced with either a real type or
574 /// a forward declaration.
575 auto TypeEntry = std::make_unique<BTFTypeDerived>(DTy, Tag, true);
576 auto &Fixup = FixupDerivedTypes[CTy->getName()];
577 Fixup.first = CTag == dwarf::DW_TAG_union_type;
578 Fixup.second.push_back(TypeEntry.get());
579 TypeId = addType(std::move(TypeEntry), DTy);
586 if (Tag == dwarf::DW_TAG_pointer_type || Tag == dwarf::DW_TAG_typedef ||
587 Tag == dwarf::DW_TAG_const_type || Tag == dwarf::DW_TAG_volatile_type ||
588 Tag == dwarf::DW_TAG_restrict_type) {
589 auto TypeEntry = std::make_unique<BTFTypeDerived>(DTy, Tag, false);
590 TypeId = addType(std::move(TypeEntry), DTy);
591 } else if (Tag != dwarf::DW_TAG_member) {
595 // Visit base type of pointer, typedef, const, volatile, restrict or
596 // struct/union member.
597 uint32_t TempTypeId = 0;
598 if (Tag == dwarf::DW_TAG_member)
599 visitTypeEntry(DTy->getBaseType(), TempTypeId, true, false);
601 visitTypeEntry(DTy->getBaseType(), TempTypeId, CheckPointer, SeenPointer);
604 void BTFDebug::visitTypeEntry(const DIType *Ty, uint32_t &TypeId,
605 bool CheckPointer, bool SeenPointer) {
606 if (!Ty || DIToIdMap.find(Ty) != DIToIdMap.end()) {
607 TypeId = DIToIdMap[Ty];
609 // To handle the case like the following:
611 // typedef struct t _t;
612 // struct s1 { _t *c; };
613 // int test1(struct s1 *arg) { ... }
615 // struct t { int a; int b; };
616 // struct s2 { _t c; }
617 // int test2(struct s2 *arg) { ... }
619 // During traversing test1() argument, "_t" is recorded
620 // in DIToIdMap and a forward declaration fixup is created
621 // for "struct t" to avoid pointee type traversal.
623 // During traversing test2() argument, even if we see "_t" is
624 // already defined, we should keep moving to eventually
625 // bring in types for "struct t". Otherwise, the "struct s2"
626 // definition won't be correct.
627 if (Ty && (!CheckPointer || !SeenPointer)) {
628 if (const auto *DTy = dyn_cast<DIDerivedType>(Ty)) {
629 unsigned Tag = DTy->getTag();
630 if (Tag == dwarf::DW_TAG_typedef || Tag == dwarf::DW_TAG_const_type ||
631 Tag == dwarf::DW_TAG_volatile_type ||
632 Tag == dwarf::DW_TAG_restrict_type) {
634 visitTypeEntry(DTy->getBaseType(), TmpTypeId, CheckPointer,
643 if (const auto *BTy = dyn_cast<DIBasicType>(Ty))
644 visitBasicType(BTy, TypeId);
645 else if (const auto *STy = dyn_cast<DISubroutineType>(Ty))
646 visitSubroutineType(STy, false, std::unordered_map<uint32_t, StringRef>(),
648 else if (const auto *CTy = dyn_cast<DICompositeType>(Ty))
649 visitCompositeType(CTy, TypeId);
650 else if (const auto *DTy = dyn_cast<DIDerivedType>(Ty))
651 visitDerivedType(DTy, TypeId, CheckPointer, SeenPointer);
653 llvm_unreachable("Unknown DIType");
656 void BTFDebug::visitTypeEntry(const DIType *Ty) {
658 visitTypeEntry(Ty, TypeId, false, false);
661 void BTFDebug::visitMapDefType(const DIType *Ty, uint32_t &TypeId) {
662 if (!Ty || DIToIdMap.find(Ty) != DIToIdMap.end()) {
663 TypeId = DIToIdMap[Ty];
667 // MapDef type may be a struct type or a non-pointer derived type
668 const DIType *OrigTy = Ty;
669 while (auto *DTy = dyn_cast<DIDerivedType>(Ty)) {
670 auto Tag = DTy->getTag();
671 if (Tag != dwarf::DW_TAG_typedef && Tag != dwarf::DW_TAG_const_type &&
672 Tag != dwarf::DW_TAG_volatile_type &&
673 Tag != dwarf::DW_TAG_restrict_type)
675 Ty = DTy->getBaseType();
678 const auto *CTy = dyn_cast<DICompositeType>(Ty);
682 auto Tag = CTy->getTag();
683 if (Tag != dwarf::DW_TAG_structure_type || CTy->isForwardDecl())
686 // Visit all struct members to ensure pointee type is visited
687 const DINodeArray Elements = CTy->getElements();
688 for (const auto *Element : Elements) {
689 const auto *MemberType = cast<DIDerivedType>(Element);
690 visitTypeEntry(MemberType->getBaseType());
693 // Visit this type, struct or a const/typedef/volatile/restrict type
694 visitTypeEntry(OrigTy, TypeId, false, false);
697 /// Read file contents from the actual file or from the source
698 std::string BTFDebug::populateFileContent(const DISubprogram *SP) {
699 auto File = SP->getFile();
700 std::string FileName;
702 if (!File->getFilename().startswith("/") && File->getDirectory().size())
703 FileName = File->getDirectory().str() + "/" + File->getFilename().str();
705 FileName = std::string(File->getFilename());
707 // No need to populate the contends if it has been populated!
708 if (FileContent.find(FileName) != FileContent.end())
711 std::vector<std::string> Content;
713 Content.push_back(Line); // Line 0 for empty string
715 std::unique_ptr<MemoryBuffer> Buf;
716 auto Source = File->getSource();
718 Buf = MemoryBuffer::getMemBufferCopy(*Source);
719 else if (ErrorOr<std::unique_ptr<MemoryBuffer>> BufOrErr =
720 MemoryBuffer::getFile(FileName))
721 Buf = std::move(*BufOrErr);
723 for (line_iterator I(*Buf, false), E; I != E; ++I)
724 Content.push_back(std::string(*I));
726 FileContent[FileName] = Content;
730 void BTFDebug::constructLineInfo(const DISubprogram *SP, MCSymbol *Label,
731 uint32_t Line, uint32_t Column) {
732 std::string FileName = populateFileContent(SP);
733 BTFLineInfo LineInfo;
735 LineInfo.Label = Label;
736 LineInfo.FileNameOff = addString(FileName);
737 // If file content is not available, let LineOff = 0.
738 if (Line < FileContent[FileName].size())
739 LineInfo.LineOff = addString(FileContent[FileName][Line]);
741 LineInfo.LineOff = 0;
742 LineInfo.LineNum = Line;
743 LineInfo.ColumnNum = Column;
744 LineInfoTable[SecNameOff].push_back(LineInfo);
747 void BTFDebug::emitCommonHeader() {
748 OS.AddComment("0x" + Twine::utohexstr(BTF::MAGIC));
749 OS.emitIntValue(BTF::MAGIC, 2);
750 OS.emitInt8(BTF::VERSION);
754 void BTFDebug::emitBTFSection() {
755 // Do not emit section if no types and only "" string.
756 if (!TypeEntries.size() && StringTable.getSize() == 1)
759 MCContext &Ctx = OS.getContext();
760 OS.SwitchSection(Ctx.getELFSection(".BTF", ELF::SHT_PROGBITS, 0));
764 OS.emitInt32(BTF::HeaderSize);
766 uint32_t TypeLen = 0, StrLen;
767 for (const auto &TypeEntry : TypeEntries)
768 TypeLen += TypeEntry->getSize();
769 StrLen = StringTable.getSize();
772 OS.emitInt32(TypeLen);
773 OS.emitInt32(TypeLen);
774 OS.emitInt32(StrLen);
777 for (const auto &TypeEntry : TypeEntries)
778 TypeEntry->emitType(OS);
780 // Emit string table.
781 uint32_t StringOffset = 0;
782 for (const auto &S : StringTable.getTable()) {
783 OS.AddComment("string offset=" + std::to_string(StringOffset));
785 OS.emitBytes(StringRef("\0", 1));
786 StringOffset += S.size() + 1;
790 void BTFDebug::emitBTFExtSection() {
791 // Do not emit section if empty FuncInfoTable and LineInfoTable
792 // and FieldRelocTable.
793 if (!FuncInfoTable.size() && !LineInfoTable.size() &&
794 !FieldRelocTable.size())
797 MCContext &Ctx = OS.getContext();
798 OS.SwitchSection(Ctx.getELFSection(".BTF.ext", ELF::SHT_PROGBITS, 0));
802 OS.emitInt32(BTF::ExtHeaderSize);
804 // Account for FuncInfo/LineInfo record size as well.
805 uint32_t FuncLen = 4, LineLen = 4;
806 // Do not account for optional FieldReloc.
807 uint32_t FieldRelocLen = 0;
808 for (const auto &FuncSec : FuncInfoTable) {
809 FuncLen += BTF::SecFuncInfoSize;
810 FuncLen += FuncSec.second.size() * BTF::BPFFuncInfoSize;
812 for (const auto &LineSec : LineInfoTable) {
813 LineLen += BTF::SecLineInfoSize;
814 LineLen += LineSec.second.size() * BTF::BPFLineInfoSize;
816 for (const auto &FieldRelocSec : FieldRelocTable) {
817 FieldRelocLen += BTF::SecFieldRelocSize;
818 FieldRelocLen += FieldRelocSec.second.size() * BTF::BPFFieldRelocSize;
825 OS.emitInt32(FuncLen);
826 OS.emitInt32(FuncLen);
827 OS.emitInt32(LineLen);
828 OS.emitInt32(FuncLen + LineLen);
829 OS.emitInt32(FieldRelocLen);
831 // Emit func_info table.
832 OS.AddComment("FuncInfo");
833 OS.emitInt32(BTF::BPFFuncInfoSize);
834 for (const auto &FuncSec : FuncInfoTable) {
835 OS.AddComment("FuncInfo section string offset=" +
836 std::to_string(FuncSec.first));
837 OS.emitInt32(FuncSec.first);
838 OS.emitInt32(FuncSec.second.size());
839 for (const auto &FuncInfo : FuncSec.second) {
840 Asm->emitLabelReference(FuncInfo.Label, 4);
841 OS.emitInt32(FuncInfo.TypeId);
845 // Emit line_info table.
846 OS.AddComment("LineInfo");
847 OS.emitInt32(BTF::BPFLineInfoSize);
848 for (const auto &LineSec : LineInfoTable) {
849 OS.AddComment("LineInfo section string offset=" +
850 std::to_string(LineSec.first));
851 OS.emitInt32(LineSec.first);
852 OS.emitInt32(LineSec.second.size());
853 for (const auto &LineInfo : LineSec.second) {
854 Asm->emitLabelReference(LineInfo.Label, 4);
855 OS.emitInt32(LineInfo.FileNameOff);
856 OS.emitInt32(LineInfo.LineOff);
857 OS.AddComment("Line " + std::to_string(LineInfo.LineNum) + " Col " +
858 std::to_string(LineInfo.ColumnNum));
859 OS.emitInt32(LineInfo.LineNum << 10 | LineInfo.ColumnNum);
863 // Emit field reloc table.
865 OS.AddComment("FieldReloc");
866 OS.emitInt32(BTF::BPFFieldRelocSize);
867 for (const auto &FieldRelocSec : FieldRelocTable) {
868 OS.AddComment("Field reloc section string offset=" +
869 std::to_string(FieldRelocSec.first));
870 OS.emitInt32(FieldRelocSec.first);
871 OS.emitInt32(FieldRelocSec.second.size());
872 for (const auto &FieldRelocInfo : FieldRelocSec.second) {
873 Asm->emitLabelReference(FieldRelocInfo.Label, 4);
874 OS.emitInt32(FieldRelocInfo.TypeID);
875 OS.emitInt32(FieldRelocInfo.OffsetNameOff);
876 OS.emitInt32(FieldRelocInfo.RelocKind);
882 void BTFDebug::beginFunctionImpl(const MachineFunction *MF) {
883 auto *SP = MF->getFunction().getSubprogram();
884 auto *Unit = SP->getUnit();
886 if (Unit->getEmissionKind() == DICompileUnit::NoDebug) {
887 SkipInstruction = true;
890 SkipInstruction = false;
892 // Collect MapDef types. Map definition needs to collect
893 // pointee types. Do it first. Otherwise, for the following
899 // foo(struct t *arg);
905 // } __attribute__((section(".maps"))) hash_map;
907 // If subroutine foo is traversed first, a type chain
908 // "ptr->struct m(fwd)" will be created and later on
909 // when traversing mapdef, since "ptr->struct m" exists,
910 // the traversal of "struct m" will be omitted.
911 if (MapDefNotCollected) {
912 processGlobals(true);
913 MapDefNotCollected = false;
916 // Collect all types locally referenced in this function.
917 // Use RetainedNodes so we can collect all argument names
918 // even if the argument is not used.
919 std::unordered_map<uint32_t, StringRef> FuncArgNames;
920 for (const DINode *DN : SP->getRetainedNodes()) {
921 if (const auto *DV = dyn_cast<DILocalVariable>(DN)) {
922 // Collect function arguments for subprogram func type.
923 uint32_t Arg = DV->getArg();
925 visitTypeEntry(DV->getType());
926 FuncArgNames[Arg] = DV->getName();
931 // Construct subprogram func proto type.
932 uint32_t ProtoTypeId;
933 visitSubroutineType(SP->getType(), true, FuncArgNames, ProtoTypeId);
935 // Construct subprogram func type
936 uint8_t Scope = SP->isLocalToUnit() ? BTF::FUNC_STATIC : BTF::FUNC_GLOBAL;
938 std::make_unique<BTFTypeFunc>(SP->getName(), ProtoTypeId, Scope);
939 uint32_t FuncTypeId = addType(std::move(FuncTypeEntry));
941 for (const auto &TypeEntry : TypeEntries)
942 TypeEntry->completeType(*this);
944 // Construct funcinfo and the first lineinfo for the function.
945 MCSymbol *FuncLabel = Asm->getFunctionBegin();
946 BTFFuncInfo FuncInfo;
947 FuncInfo.Label = FuncLabel;
948 FuncInfo.TypeId = FuncTypeId;
949 if (FuncLabel->isInSection()) {
950 MCSection &Section = FuncLabel->getSection();
951 const MCSectionELF *SectionELF = dyn_cast<MCSectionELF>(&Section);
952 assert(SectionELF && "Null section for Function Label");
953 SecNameOff = addString(SectionELF->getName());
955 SecNameOff = addString(".text");
957 FuncInfoTable[SecNameOff].push_back(FuncInfo);
960 void BTFDebug::endFunctionImpl(const MachineFunction *MF) {
961 SkipInstruction = false;
962 LineInfoGenerated = false;
966 /// On-demand populate types as requested from abstract member
967 /// accessing or preserve debuginfo type.
968 unsigned BTFDebug::populateType(const DIType *Ty) {
970 visitTypeEntry(Ty, Id, false, false);
971 for (const auto &TypeEntry : TypeEntries)
972 TypeEntry->completeType(*this);
976 /// Generate a struct member field relocation.
977 void BTFDebug::generatePatchImmReloc(const MCSymbol *ORSym, uint32_t RootId,
978 const GlobalVariable *GVar, bool IsAma) {
979 BTFFieldReloc FieldReloc;
980 FieldReloc.Label = ORSym;
981 FieldReloc.TypeID = RootId;
983 StringRef AccessPattern = GVar->getName();
984 size_t FirstDollar = AccessPattern.find_first_of('$');
986 size_t FirstColon = AccessPattern.find_first_of(':');
987 size_t SecondColon = AccessPattern.find_first_of(':', FirstColon + 1);
988 StringRef IndexPattern = AccessPattern.substr(FirstDollar + 1);
989 StringRef RelocKindStr = AccessPattern.substr(FirstColon + 1,
990 SecondColon - FirstColon);
991 StringRef PatchImmStr = AccessPattern.substr(SecondColon + 1,
992 FirstDollar - SecondColon);
994 FieldReloc.OffsetNameOff = addString(IndexPattern);
995 FieldReloc.RelocKind = std::stoull(std::string(RelocKindStr));
996 PatchImms[GVar] = std::stoul(std::string(PatchImmStr));
998 StringRef RelocStr = AccessPattern.substr(FirstDollar + 1);
999 FieldReloc.OffsetNameOff = addString("0");
1000 FieldReloc.RelocKind = std::stoull(std::string(RelocStr));
1001 PatchImms[GVar] = RootId;
1003 FieldRelocTable[SecNameOff].push_back(FieldReloc);
1006 void BTFDebug::processReloc(const MachineOperand &MO) {
1007 // check whether this is a candidate or not
1008 if (MO.isGlobal()) {
1009 const GlobalValue *GVal = MO.getGlobal();
1010 auto *GVar = dyn_cast<GlobalVariable>(GVal);
1014 if (!GVar->hasAttribute(BPFCoreSharedInfo::AmaAttr) &&
1015 !GVar->hasAttribute(BPFCoreSharedInfo::TypeIdAttr))
1018 MCSymbol *ORSym = OS.getContext().createTempSymbol();
1019 OS.emitLabel(ORSym);
1021 MDNode *MDN = GVar->getMetadata(LLVMContext::MD_preserve_access_index);
1022 uint32_t RootId = populateType(dyn_cast<DIType>(MDN));
1023 generatePatchImmReloc(ORSym, RootId, GVar,
1024 GVar->hasAttribute(BPFCoreSharedInfo::AmaAttr));
1028 void BTFDebug::beginInstruction(const MachineInstr *MI) {
1029 DebugHandlerBase::beginInstruction(MI);
1031 if (SkipInstruction || MI->isMetaInstruction() ||
1032 MI->getFlag(MachineInstr::FrameSetup))
1035 if (MI->isInlineAsm()) {
1036 // Count the number of register definitions to find the asm string.
1037 unsigned NumDefs = 0;
1038 for (; MI->getOperand(NumDefs).isReg() && MI->getOperand(NumDefs).isDef();
1042 // Skip this inline asm instruction if the asmstr is empty.
1043 const char *AsmStr = MI->getOperand(NumDefs).getSymbolName();
1048 if (MI->getOpcode() == BPF::LD_imm64) {
1049 // If the insn is "r2 = LD_imm64 @<an AmaAttr global>",
1050 // add this insn into the .BTF.ext FieldReloc subsection.
1051 // Relocation looks like:
1057 // Later, the insn is replaced with "r2 = <offset>"
1058 // where "<offset>" equals to the offset based on current
1059 // type definitions.
1061 // If the insn is "r2 = LD_imm64 @<an TypeIdAttr global>",
1062 // The LD_imm64 result will be replaced with a btf type id.
1063 processReloc(MI->getOperand(1));
1064 } else if (MI->getOpcode() == BPF::CORE_MEM ||
1065 MI->getOpcode() == BPF::CORE_ALU32_MEM ||
1066 MI->getOpcode() == BPF::CORE_SHIFT) {
1067 // relocation insn is a load, store or shift insn.
1068 processReloc(MI->getOperand(3));
1069 } else if (MI->getOpcode() == BPF::JAL) {
1070 // check extern function references
1071 const MachineOperand &MO = MI->getOperand(0);
1072 if (MO.isGlobal()) {
1073 processFuncPrototypes(dyn_cast<Function>(MO.getGlobal()));
1077 // Skip this instruction if no DebugLoc or the DebugLoc
1078 // is the same as the previous instruction.
1079 const DebugLoc &DL = MI->getDebugLoc();
1080 if (!DL || PrevInstLoc == DL) {
1081 // This instruction will be skipped, no LineInfo has
1082 // been generated, construct one based on function signature.
1083 if (LineInfoGenerated == false) {
1084 auto *S = MI->getMF()->getFunction().getSubprogram();
1085 MCSymbol *FuncLabel = Asm->getFunctionBegin();
1086 constructLineInfo(S, FuncLabel, S->getLine(), 0);
1087 LineInfoGenerated = true;
1093 // Create a temporary label to remember the insn for lineinfo.
1094 MCSymbol *LineSym = OS.getContext().createTempSymbol();
1095 OS.emitLabel(LineSym);
1097 // Construct the lineinfo.
1098 auto SP = DL.get()->getScope()->getSubprogram();
1099 constructLineInfo(SP, LineSym, DL.getLine(), DL.getCol());
1101 LineInfoGenerated = true;
1105 void BTFDebug::processGlobals(bool ProcessingMapDef) {
1106 // Collect all types referenced by globals.
1107 const Module *M = MMI->getModule();
1108 for (const GlobalVariable &Global : M->globals()) {
1109 // Decide the section name.
1111 if (Global.hasSection()) {
1112 SecName = Global.getSection();
1113 } else if (Global.hasInitializer()) {
1114 // data, bss, or readonly sections
1115 if (Global.isConstant())
1116 SecName = ".rodata";
1118 SecName = Global.getInitializer()->isZeroValue() ? ".bss" : ".data";
1120 // extern variables without explicit section,
1121 // put them into ".extern" section.
1122 SecName = ".extern";
1125 if (ProcessingMapDef != SecName.startswith(".maps"))
1128 SmallVector<DIGlobalVariableExpression *, 1> GVs;
1129 Global.getDebugInfo(GVs);
1131 // No type information, mostly internal, skip it.
1132 if (GVs.size() == 0)
1135 uint32_t GVTypeId = 0;
1136 for (auto *GVE : GVs) {
1137 if (SecName.startswith(".maps"))
1138 visitMapDefType(GVE->getVariable()->getType(), GVTypeId);
1140 visitTypeEntry(GVE->getVariable()->getType(), GVTypeId, false, false);
1144 // Only support the following globals:
1145 // . static variables
1146 // . non-static weak or non-weak global variables
1147 // . weak or non-weak extern global variables
1148 // Whether DataSec is readonly or not can be found from corresponding ELF
1149 // section flags. Whether a BTF_KIND_VAR is a weak symbol or not
1150 // can be found from the corresponding ELF symbol table.
1151 auto Linkage = Global.getLinkage();
1152 if (Linkage != GlobalValue::InternalLinkage &&
1153 Linkage != GlobalValue::ExternalLinkage &&
1154 Linkage != GlobalValue::WeakAnyLinkage &&
1155 Linkage != GlobalValue::ExternalWeakLinkage)
1159 if (Linkage == GlobalValue::InternalLinkage) {
1160 GVarInfo = BTF::VAR_STATIC;
1161 } else if (Global.hasInitializer()) {
1162 GVarInfo = BTF::VAR_GLOBAL_ALLOCATED;
1164 GVarInfo = BTF::VAR_GLOBAL_EXTERNAL;
1168 std::make_unique<BTFKindVar>(Global.getName(), GVTypeId, GVarInfo);
1169 uint32_t VarId = addType(std::move(VarEntry));
1171 assert(!SecName.empty());
1173 // Find or create a DataSec
1174 if (DataSecEntries.find(std::string(SecName)) == DataSecEntries.end()) {
1175 DataSecEntries[std::string(SecName)] =
1176 std::make_unique<BTFKindDataSec>(Asm, std::string(SecName));
1179 // Calculate symbol size
1180 const DataLayout &DL = Global.getParent()->getDataLayout();
1181 uint32_t Size = DL.getTypeAllocSize(Global.getType()->getElementType());
1183 DataSecEntries[std::string(SecName)]->addVar(VarId, Asm->getSymbol(&Global),
1188 /// Emit proper patchable instructions.
1189 bool BTFDebug::InstLower(const MachineInstr *MI, MCInst &OutMI) {
1190 if (MI->getOpcode() == BPF::LD_imm64) {
1191 const MachineOperand &MO = MI->getOperand(1);
1192 if (MO.isGlobal()) {
1193 const GlobalValue *GVal = MO.getGlobal();
1194 auto *GVar = dyn_cast<GlobalVariable>(GVal);
1196 // Emit "mov ri, <imm>"
1198 if (GVar->hasAttribute(BPFCoreSharedInfo::AmaAttr) ||
1199 GVar->hasAttribute(BPFCoreSharedInfo::TypeIdAttr))
1200 Imm = PatchImms[GVar];
1204 OutMI.setOpcode(BPF::MOV_ri);
1205 OutMI.addOperand(MCOperand::createReg(MI->getOperand(0).getReg()));
1206 OutMI.addOperand(MCOperand::createImm(Imm));
1210 } else if (MI->getOpcode() == BPF::CORE_MEM ||
1211 MI->getOpcode() == BPF::CORE_ALU32_MEM ||
1212 MI->getOpcode() == BPF::CORE_SHIFT) {
1213 const MachineOperand &MO = MI->getOperand(3);
1214 if (MO.isGlobal()) {
1215 const GlobalValue *GVal = MO.getGlobal();
1216 auto *GVar = dyn_cast<GlobalVariable>(GVal);
1217 if (GVar && GVar->hasAttribute(BPFCoreSharedInfo::AmaAttr)) {
1218 uint32_t Imm = PatchImms[GVar];
1219 OutMI.setOpcode(MI->getOperand(1).getImm());
1220 if (MI->getOperand(0).isImm())
1221 OutMI.addOperand(MCOperand::createImm(MI->getOperand(0).getImm()));
1223 OutMI.addOperand(MCOperand::createReg(MI->getOperand(0).getReg()));
1224 OutMI.addOperand(MCOperand::createReg(MI->getOperand(2).getReg()));
1225 OutMI.addOperand(MCOperand::createImm(Imm));
1233 void BTFDebug::processFuncPrototypes(const Function *F) {
1237 const DISubprogram *SP = F->getSubprogram();
1238 if (!SP || SP->isDefinition())
1241 // Do not emit again if already emitted.
1242 if (ProtoFunctions.find(F) != ProtoFunctions.end())
1244 ProtoFunctions.insert(F);
1246 uint32_t ProtoTypeId;
1247 const std::unordered_map<uint32_t, StringRef> FuncArgNames;
1248 visitSubroutineType(SP->getType(), false, FuncArgNames, ProtoTypeId);
1250 uint8_t Scope = BTF::FUNC_EXTERN;
1251 auto FuncTypeEntry =
1252 std::make_unique<BTFTypeFunc>(SP->getName(), ProtoTypeId, Scope);
1253 addType(std::move(FuncTypeEntry));
1256 void BTFDebug::endModule() {
1257 // Collect MapDef globals if not collected yet.
1258 if (MapDefNotCollected) {
1259 processGlobals(true);
1260 MapDefNotCollected = false;
1263 // Collect global types/variables except MapDef globals.
1264 processGlobals(false);
1266 for (auto &DataSec : DataSecEntries)
1267 addType(std::move(DataSec.second));
1270 for (auto &Fixup : FixupDerivedTypes) {
1271 StringRef TypeName = Fixup.first;
1272 bool IsUnion = Fixup.second.first;
1274 // Search through struct types
1275 uint32_t StructTypeId = 0;
1276 for (const auto &StructType : StructTypes) {
1277 if (StructType->getName() == TypeName) {
1278 StructTypeId = StructType->getId();
1283 if (StructTypeId == 0) {
1284 auto FwdTypeEntry = std::make_unique<BTFTypeFwd>(TypeName, IsUnion);
1285 StructTypeId = addType(std::move(FwdTypeEntry));
1288 for (auto &DType : Fixup.second.second) {
1289 DType->setPointeeType(StructTypeId);
1293 // Complete BTF type cross refereences.
1294 for (const auto &TypeEntry : TypeEntries)
1295 TypeEntry->completeType(*this);
1297 // Emit BTF sections.
1299 emitBTFExtSection();