37#define HANDLE_BTF_KIND(ID, NAME) "BTF_KIND_" #NAME,
38#include "llvm/DebugInfo/BTF/BTF.def"
45 if (DerivedTy && DerivedTy->getTag() == dwarf::DW_TAG_atomic_type)
46 return DerivedTy->getBaseType();
62 : DTy(DTy), NeedsFixup(NeedsFixup), Name(DTy->
getName()) {
64 case dwarf::DW_TAG_pointer_type:
65 Kind = BTF::BTF_KIND_PTR;
67 case dwarf::DW_TAG_const_type:
68 Kind = BTF::BTF_KIND_CONST;
70 case dwarf::DW_TAG_volatile_type:
71 Kind = BTF::BTF_KIND_VOLATILE;
73 case dwarf::DW_TAG_typedef:
74 Kind = BTF::BTF_KIND_TYPEDEF;
76 case dwarf::DW_TAG_restrict_type:
77 Kind = BTF::BTF_KIND_RESTRICT;
88 : DTy(nullptr), NeedsFixup(
false), Name(Name) {
89 Kind = BTF::BTF_KIND_PTR;
100 case BTF::BTF_KIND_PTR:
101 case BTF::BTF_KIND_CONST:
102 case BTF::BTF_KIND_VOLATILE:
103 case BTF::BTF_KIND_RESTRICT:
118 if (NeedsFixup || !DTy)
124 assert((
Kind == BTF::BTF_KIND_PTR ||
Kind == BTF::BTF_KIND_CONST ||
125 Kind == BTF::BTF_KIND_VOLATILE) &&
126 "Invalid null basetype");
141 Kind = BTF::BTF_KIND_FWD;
162 case dwarf::DW_ATE_boolean:
165 case dwarf::DW_ATE_signed:
166 case dwarf::DW_ATE_signed_char:
169 case dwarf::DW_ATE_unsigned:
170 case dwarf::DW_ATE_unsigned_char:
177 Kind = BTF::BTF_KIND_INT;
180 IntVal = (BTFEncoding << 24) | OffsetInBits << 16 | SizeInBits;
198 bool IsSigned) : ETy(ETy) {
199 Kind = BTF::BTF_KIND_ENUM;
211 DINodeArray Elements = ETy->getElements();
212 for (
const auto Element : Elements) {
219 if (Enum->isUnsigned())
220 Value =
static_cast<uint32_t>(Enum->getValue().getZExtValue());
222 Value =
static_cast<uint32_t>(Enum->getValue().getSExtValue());
224 EnumValues.push_back(BTFEnum);
230 for (
const auto &Enum : EnumValues) {
237 bool IsSigned) : ETy(ETy) {
238 Kind = BTF::BTF_KIND_ENUM64;
250 DINodeArray Elements = ETy->getElements();
251 for (
const auto Element : Elements) {
257 if (Enum->isUnsigned())
258 Value = Enum->getValue().getZExtValue();
260 Value =
static_cast<uint64_t>(Enum->getValue().getSExtValue());
263 EnumValues.push_back(BTFEnum);
269 for (
const auto &Enum : EnumValues) {
279 Kind = BTF::BTF_KIND_ARRAY;
284 ArrayInfo.ElemType = ElemTypeId;
285 ArrayInfo.Nelems = NumElems;
311 : STy(STy), HasBitField(HasBitField) {
312 Kind = IsStruct ? BTF::BTF_KIND_STRUCT : BTF::BTF_KIND_UNION;
314 BTFType.Info = (HasBitField << 31) | (
Kind << 24) | Vlen;
324 if (STy->getTag() == dwarf::DW_TAG_variant_part) {
333 const auto *DTy = STy->getDiscriminator();
337 Discriminator.NameOff = BDebug.
addString(DTy->getName());
338 Discriminator.Offset = DTy->getOffsetInBits();
339 const auto *BaseTy = DTy->getBaseType();
340 Discriminator.Type = BDebug.
getTypeId(BaseTy);
342 Members.push_back(Discriminator);
347 const DINodeArray Elements = STy->getElements();
348 for (
const auto *Element : Elements) {
351 switch (Element->getTag()) {
352 case dwarf::DW_TAG_member: {
357 uint8_t BitFieldSize = DDTy->isBitField() ? DDTy->getSizeInBits() : 0;
358 BTFMember.
Offset = BitFieldSize << 24 | DDTy->getOffsetInBits();
360 BTFMember.
Offset = DDTy->getOffsetInBits();
366 case dwarf::DW_TAG_variant_part: {
370 BTFMember.
Offset = DCTy->getOffsetInBits();
377 Members.push_back(BTFMember);
383 for (
const auto &Member : Members) {
400 const std::unordered_map<uint32_t, StringRef> &FuncArgNames)
401 : STy(STy), FuncArgNames(FuncArgNames) {
402 Kind = BTF::BTF_KIND_FUNC_PROTO;
418 for (
unsigned I = 1,
N = Elements.size();
I <
N; ++
I) {
422 Param.NameOff = BDebug.
addString(FuncArgNames[
I]);
428 Parameters.push_back(Param);
434 for (
const auto &Param : Parameters) {
443 Kind = BTF::BTF_KIND_FUNC;
460 Kind = BTF::BTF_KIND_VAR;
476 : Asm(AsmPrt), Name(SecName) {
477 Kind = BTF::BTF_KIND_DATASEC;
490 for (
const auto &V : Vars) {
492 Asm->emitLabelReference(std::get<1>(V), 4);
499 Kind = BTF::BTF_KIND_FLOAT;
515 Kind = BTF::BTF_KIND_DECL_TAG;
535 : DTy(nullptr), Tag(Tag) {
536 Kind = BTF::BTF_KIND_TYPE_TAG;
542 : DTy(DTy), Tag(Tag) {
543 Kind = BTF::BTF_KIND_TYPE_TAG;
563 for (
auto &OffsetM : OffsetToIdMap) {
564 if (Table[OffsetM.second] == S)
565 return OffsetM.first;
569 OffsetToIdMap[
Offset] = Table.size();
570 Table.push_back(std::string(S));
571 Size += S.
size() + 1;
577 LineInfoGenerated(
false), SecNameOff(0), ArrayIndexTypeId(0),
578 MapDefNotCollected(
true) {
582uint32_t BTFDebug::addType(std::unique_ptr<BTFTypeBase> TypeEntry,
584 TypeEntry->setId(TypeEntries.size() + 1);
587 TypeEntries.push_back(std::move(TypeEntry));
591uint32_t BTFDebug::addType(std::unique_ptr<BTFTypeBase> TypeEntry) {
592 TypeEntry->setId(TypeEntries.size() + 1);
594 TypeEntries.push_back(std::move(TypeEntry));
598void BTFDebug::visitBasicType(
const DIBasicType *BTy, uint32_t &TypeId) {
603 case dwarf::DW_ATE_boolean:
604 case dwarf::DW_ATE_signed:
605 case dwarf::DW_ATE_signed_char:
606 case dwarf::DW_ATE_unsigned:
607 case dwarf::DW_ATE_unsigned_char:
610 TypeEntry = std::make_unique<BTFTypeInt>(
613 case dwarf::DW_ATE_float:
621 TypeId = addType(std::move(TypeEntry), BTy);
625void BTFDebug::visitSubroutineType(
627 const std::unordered_map<uint32_t, StringRef> &FuncArgNames,
630 uint32_t VLen =
Elements.size() - 1;
638 auto TypeEntry = std::make_unique<BTFTypeFuncProto>(STy, VLen, FuncArgNames);
640 TypeId = addType(std::move(TypeEntry));
642 TypeId = addType(std::move(TypeEntry), STy);
645 for (
const auto Element : Elements) {
646 visitTypeEntry(Element);
650void BTFDebug::processDeclAnnotations(DINodeArray
Annotations,
656 for (
const Metadata *Annotation : Annotations->operands()) {
659 if (
Name->getString() !=
"btf_decl_tag")
663 auto TypeEntry = std::make_unique<BTFTypeDeclTag>(BaseTypeId, ComponentIdx,
665 addType(std::move(TypeEntry));
669uint32_t BTFDebug::processDISubprogram(
const DISubprogram *SP,
670 uint32_t ProtoTypeId, uint8_t Scope) {
672 std::make_unique<BTFTypeFunc>(
SP->getName(), ProtoTypeId, Scope);
673 uint32_t FuncId = addType(std::move(FuncTypeEntry));
676 for (
const DINode *DN :
SP->getRetainedNodes()) {
678 uint32_t Arg = DV->getArg();
680 processDeclAnnotations(DV->getAnnotations(), FuncId, Arg - 1);
683 processDeclAnnotations(
SP->getAnnotations(), FuncId, -1);
689int BTFDebug::genBTFTypeTags(
const DIDerivedType *DTy,
int BaseTypeId) {
695 for (
const Metadata *Annotations : Annots->operands()) {
698 if (
Name->getString() !=
"btf_type_tag")
704 if (MDStrs.
size() == 0)
712 std::unique_ptr<BTFTypeTypeTag>
TypeEntry;
715 std::make_unique<BTFTypeTypeTag>(BaseTypeId, MDStrs[0]->getString());
717 TypeEntry = std::make_unique<BTFTypeTypeTag>(DTy, MDStrs[0]->getString());
718 TmpTypeId = addType(std::move(TypeEntry));
720 for (
unsigned I = 1;
I < MDStrs.
size();
I++) {
721 const MDString *
Value = MDStrs[
I];
722 TypeEntry = std::make_unique<BTFTypeTypeTag>(TmpTypeId,
Value->getString());
723 TmpTypeId = addType(std::move(TypeEntry));
729void BTFDebug::visitStructType(
const DICompositeType *CTy,
bool IsStruct,
736 if (CTy->
getTag() == dwarf::DW_TAG_variant_part) {
747 bool HasBitField =
false;
748 for (
const auto *Element : Elements) {
749 if (Element->getTag() == dwarf::DW_TAG_member) {
751 if (
E->isBitField()) {
759 std::make_unique<BTFTypeStruct>(CTy, IsStruct, HasBitField, VLen);
761 TypeId = addType(std::move(TypeEntry), CTy);
768 for (
const auto *Element : Elements) {
769 switch (Element->getTag()) {
770 case dwarf::DW_TAG_member: {
772 visitTypeEntry(Elem);
773 processDeclAnnotations(Elem->getAnnotations(), TypeId, FieldNo);
776 case dwarf::DW_TAG_variant_part: {
778 visitTypeEntry(Elem);
779 processDeclAnnotations(Elem->getAnnotations(), TypeId, FieldNo);
789void BTFDebug::visitArrayType(
const DICompositeType *CTy, uint32_t &TypeId) {
793 visitTypeEntry(ElemType, ElemTypeId,
false,
false);
799 if (Element->getTag() == dwarf::DW_TAG_subrange_type) {
802 int64_t
Count = CI->getSExtValue();
807 std::make_unique<BTFTypeArray>(ElemTypeId,
810 ElemTypeId = addType(std::move(TypeEntry), CTy);
812 ElemTypeId = addType(std::move(TypeEntry));
821 if (!ArrayIndexTypeId) {
822 auto TypeEntry = std::make_unique<BTFTypeInt>(dwarf::DW_ATE_unsigned, 32,
823 0,
"__ARRAY_SIZE_TYPE__");
824 ArrayIndexTypeId = addType(std::move(TypeEntry));
828void BTFDebug::visitEnumType(
const DICompositeType *CTy, uint32_t &TypeId) {
834 bool IsSigned =
false;
835 unsigned NumBits = 32;
840 IsSigned = BTy->
getEncoding() == dwarf::DW_ATE_signed ||
846 auto TypeEntry = std::make_unique<BTFTypeEnum>(CTy, VLen, IsSigned);
847 TypeId = addType(std::move(TypeEntry), CTy);
850 auto TypeEntry = std::make_unique<BTFTypeEnum64>(CTy, VLen, IsSigned);
851 TypeId = addType(std::move(TypeEntry), CTy);
857void BTFDebug::visitFwdDeclType(
const DICompositeType *CTy,
bool IsUnion,
860 TypeId = addType(std::move(TypeEntry), CTy);
868 case dwarf::DW_TAG_structure_type:
869 case dwarf::DW_TAG_union_type:
870 case dwarf::DW_TAG_variant_part:
873 visitFwdDeclType(CTy,
Tag == dwarf::DW_TAG_union_type, TypeId);
875 visitStructType(CTy,
Tag == dwarf::DW_TAG_structure_type, TypeId);
877 case dwarf::DW_TAG_array_type:
878 visitArrayType(CTy, TypeId);
880 case dwarf::DW_TAG_enumeration_type:
881 visitEnumType(CTy, TypeId);
888bool BTFDebug::IsForwardDeclCandidate(
const DIType *
Base) {
890 auto CTag = CTy->
getTag();
891 if ((CTag == dwarf::DW_TAG_structure_type ||
892 CTag == dwarf::DW_TAG_union_type) &&
900void BTFDebug::visitDerivedType(
const DIDerivedType *DTy, uint32_t &TypeId,
901 bool CheckPointer,
bool SeenPointer) {
904 if (
Tag == dwarf::DW_TAG_atomic_type)
905 return visitTypeEntry(DTy->getBaseType(), TypeId, CheckPointer,
910 if (CheckPointer && !SeenPointer) {
914 if (CheckPointer && SeenPointer) {
915 const DIType *
Base = DTy->getBaseType();
917 if (IsForwardDeclCandidate(
Base)) {
921 auto TypeEntry = std::make_unique<BTFTypeDerived>(DTy,
Tag,
true);
924 TypeId = addType(std::move(TypeEntry), DTy);
930 if (
Tag == dwarf::DW_TAG_pointer_type) {
931 int TmpTypeId = genBTFTypeTags(DTy, -1);
932 if (TmpTypeId >= 0) {
934 std::make_unique<BTFTypeDerived>(TmpTypeId,
Tag, DTy->
getName());
935 TypeId = addType(std::move(TypeDEntry), DTy);
937 auto TypeEntry = std::make_unique<BTFTypeDerived>(DTy,
Tag,
false);
938 TypeId = addType(std::move(TypeEntry), DTy);
940 }
else if (
Tag == dwarf::DW_TAG_typedef ||
Tag == dwarf::DW_TAG_const_type ||
941 Tag == dwarf::DW_TAG_volatile_type ||
942 Tag == dwarf::DW_TAG_restrict_type) {
943 auto TypeEntry = std::make_unique<BTFTypeDerived>(DTy,
Tag,
false);
944 TypeId = addType(std::move(TypeEntry), DTy);
945 if (
Tag == dwarf::DW_TAG_typedef)
947 }
else if (
Tag != dwarf::DW_TAG_member) {
953 uint32_t TempTypeId = 0;
954 if (
Tag == dwarf::DW_TAG_member)
955 visitTypeEntry(DTy->getBaseType(), TempTypeId,
true,
false);
957 visitTypeEntry(DTy->getBaseType(), TempTypeId, CheckPointer, SeenPointer);
967void BTFDebug::visitTypeEntry(
const DIType *Ty, uint32_t &TypeId,
968 bool CheckPointer,
bool SeenPointer) {
969 if (!Ty || DIToIdMap.find(Ty) != DIToIdMap.end()) {
970 TypeId = DIToIdMap[Ty];
1001 if (Ty && (!CheckPointer || !SeenPointer)) {
1004 const DIType *BaseTy = DTy->getBaseType();
1008 if (DIToIdMap.find(BaseTy) != DIToIdMap.end()) {
1011 if (CheckPointer && DTy->
getTag() == dwarf::DW_TAG_pointer_type &&
1014 if (IsForwardDeclCandidate(BaseTy))
1018 visitTypeEntry(BaseTy, TmpTypeId, CheckPointer, SeenPointer);
1029 visitBasicType(BTy, TypeId);
1031 visitSubroutineType(STy,
false, std::unordered_map<uint32_t, StringRef>(),
1034 visitCompositeType(CTy, TypeId);
1036 visitDerivedType(DTy, TypeId, CheckPointer, SeenPointer);
1041void BTFDebug::visitTypeEntry(
const DIType *Ty) {
1043 visitTypeEntry(Ty, TypeId,
false,
false);
1046void BTFDebug::visitMapDefType(
const DIType *Ty, uint32_t &TypeId) {
1047 if (!Ty || DIToIdMap.find(Ty) != DIToIdMap.end()) {
1048 TypeId = DIToIdMap[Ty];
1054 case dwarf::DW_TAG_typedef:
1055 case dwarf::DW_TAG_const_type:
1056 case dwarf::DW_TAG_volatile_type:
1057 case dwarf::DW_TAG_restrict_type:
1058 case dwarf::DW_TAG_pointer_type:
1061 case dwarf::DW_TAG_array_type:
1065 case dwarf::DW_TAG_structure_type: {
1069 for (
const auto *Element : Elements) {
1071 const DIType *MemberBaseType = MemberType->getBaseType();
1080 visitMapDefType(MemberBaseType, TmpId);
1082 visitTypeEntry(MemberBaseType);
1092 visitTypeEntry(Ty, TypeId,
false,
false);
1096std::string BTFDebug::populateFileContent(
const DIFile *File) {
1097 std::string FileName;
1099 if (!
File->getFilename().starts_with(
"/") &&
File->getDirectory().size())
1100 FileName =
File->getDirectory().str() +
"/" +
File->getFilename().str();
1102 FileName = std::string(
File->getFilename());
1105 if (FileContent.contains(FileName))
1108 std::vector<std::string> Content;
1110 Content.push_back(Line);
1112 auto LoadFile = [](StringRef FileName) {
1118 std::unique_ptr<MemoryBuffer> Buf;
1122 else if (ErrorOr<std::unique_ptr<MemoryBuffer>> BufOrErr = LoadFile(FileName))
1123 Buf = std::move(*BufOrErr);
1125 for (line_iterator
I(*Buf,
false),
E;
I !=
E; ++
I)
1126 Content.push_back(std::string(*
I));
1128 FileContent[FileName] = Content;
1132void BTFDebug::constructLineInfo(
MCSymbol *Label,
const DIFile *File,
1133 uint32_t Line, uint32_t Column) {
1134 std::string FileName = populateFileContent(File);
1135 BTFLineInfo LineInfo;
1140 const auto &Content = FileContent[FileName];
1141 if (Line < Content.size())
1147 LineInfoTable[SecNameOff].push_back(LineInfo);
1150void BTFDebug::emitCommonHeader() {
1157void BTFDebug::emitBTFSection() {
1159 if (!TypeEntries.size() && StringTable.getSize() == 1)
1162 MCContext &Ctx = OS.getContext();
1165 OS.switchSection(Sec);
1171 uint32_t TypeLen = 0, StrLen;
1172 for (
const auto &TypeEntry : TypeEntries)
1174 StrLen = StringTable.getSize();
1177 OS.emitInt32(TypeLen);
1178 OS.emitInt32(TypeLen);
1179 OS.emitInt32(StrLen);
1182 for (
const auto &TypeEntry : TypeEntries)
1186 uint32_t StringOffset = 0;
1187 for (
const auto &S : StringTable.getTable()) {
1188 OS.AddComment(
"string offset=" + std::to_string(StringOffset));
1190 OS.emitBytes(StringRef(
"\0", 1));
1191 StringOffset += S.size() + 1;
1195void BTFDebug::emitBTFExtSection() {
1198 if (!FuncInfoTable.size() && !LineInfoTable.size() &&
1199 !FieldRelocTable.size())
1202 MCContext &Ctx = OS.getContext();
1205 OS.switchSection(Sec);
1212 uint32_t FuncLen = 4, LineLen = 4;
1214 uint32_t FieldRelocLen = 0;
1215 for (
const auto &FuncSec : FuncInfoTable) {
1219 for (
const auto &LineSec : LineInfoTable) {
1223 for (
const auto &FieldRelocSec : FieldRelocTable) {
1232 OS.emitInt32(FuncLen);
1233 OS.emitInt32(FuncLen);
1234 OS.emitInt32(LineLen);
1235 OS.emitInt32(FuncLen + LineLen);
1236 OS.emitInt32(FieldRelocLen);
1239 OS.AddComment(
"FuncInfo");
1241 for (
const auto &FuncSec : FuncInfoTable) {
1242 OS.AddComment(
"FuncInfo section string offset=" +
1243 std::to_string(FuncSec.first));
1244 OS.emitInt32(FuncSec.first);
1245 OS.emitInt32(FuncSec.second.size());
1246 for (
const auto &FuncInfo : FuncSec.second) {
1247 Asm->emitLabelReference(FuncInfo.Label, 4);
1248 OS.emitInt32(FuncInfo.TypeId);
1253 OS.AddComment(
"LineInfo");
1255 for (
const auto &LineSec : LineInfoTable) {
1256 OS.AddComment(
"LineInfo section string offset=" +
1257 std::to_string(LineSec.first));
1258 OS.emitInt32(LineSec.first);
1259 OS.emitInt32(LineSec.second.size());
1260 for (
const auto &LineInfo : LineSec.second) {
1261 Asm->emitLabelReference(LineInfo.
Label, 4);
1263 OS.emitInt32(LineInfo.
LineOff);
1264 OS.AddComment(
"Line " + std::to_string(LineInfo.
LineNum) +
" Col " +
1271 if (FieldRelocLen) {
1272 OS.AddComment(
"FieldReloc");
1274 for (
const auto &FieldRelocSec : FieldRelocTable) {
1275 OS.AddComment(
"Field reloc section string offset=" +
1276 std::to_string(FieldRelocSec.first));
1277 OS.emitInt32(FieldRelocSec.first);
1278 OS.emitInt32(FieldRelocSec.second.size());
1279 for (
const auto &FieldRelocInfo : FieldRelocSec.second) {
1280 Asm->emitLabelReference(FieldRelocInfo.Label, 4);
1281 OS.emitInt32(FieldRelocInfo.TypeID);
1282 OS.emitInt32(FieldRelocInfo.OffsetNameOff);
1283 OS.emitInt32(FieldRelocInfo.RelocKind);
1291 auto *Unit = SP->getUnit();
1294 SkipInstruction =
true;
1297 SkipInstruction =
false;
1318 if (MapDefNotCollected) {
1319 processGlobals(
true);
1320 MapDefNotCollected =
false;
1326 std::unordered_map<uint32_t, StringRef> FuncArgNames;
1327 for (
const DINode *DN : SP->getRetainedNodes()) {
1332 visitTypeEntry(DV->getType());
1333 FuncArgNames[Arg] = DV->getName();
1340 visitSubroutineType(SP->getType(),
true, FuncArgNames, ProtoTypeId);
1344 uint32_t FuncTypeId = processDISubprogram(SP, ProtoTypeId, Scope);
1346 for (
const auto &TypeEntry : TypeEntries)
1347 TypeEntry->completeType(*
this);
1352 FuncInfo.
Label = FuncLabel;
1353 FuncInfo.
TypeId = FuncTypeId;
1360 FuncInfoTable[SecNameOff].push_back(FuncInfo);
1364 SkipInstruction =
false;
1365 LineInfoGenerated =
false;
1371unsigned BTFDebug::populateType(
const DIType *Ty) {
1373 visitTypeEntry(Ty, Id,
false,
false);
1374 for (
const auto &TypeEntry : TypeEntries)
1375 TypeEntry->completeType(*
this);
1380void BTFDebug::generatePatchImmReloc(
const MCSymbol *ORSym,
uint32_t RootId,
1383 FieldReloc.
Label = ORSym;
1384 FieldReloc.
TypeID = RootId;
1390 size_t SecondColon = AccessPattern.
find_first_of(
':', FirstColon + 1);
1393 SecondColon - FirstColon);
1395 FirstDollar - SecondColon);
1398 FieldReloc.
RelocKind = std::stoull(std::string(RelocKindStr));
1399 PatchImms[GVar] = std::make_pair(std::stoll(std::string(PatchImmStr)),
1402 StringRef RelocStr = AccessPattern.
substr(FirstDollar + 1);
1404 FieldReloc.
RelocKind = std::stoull(std::string(RelocStr));
1405 PatchImms[GVar] = std::make_pair(RootId, FieldReloc.
RelocKind);
1407 FieldRelocTable[SecNameOff].push_back(FieldReloc);
1413 const GlobalValue *GVal = MO.
getGlobal();
1425 MCSymbol *ORSym = OS.getContext().createTempSymbol();
1426 OS.emitLabel(ORSym);
1428 MDNode *MDN = GVar->
getMetadata(LLVMContext::MD_preserve_access_index);
1430 generatePatchImmReloc(ORSym, RootId, GVar,
1438 if (SkipInstruction ||
MI->isMetaInstruction() ||
1442 if (
MI->isInlineAsm()) {
1444 unsigned NumDefs = 0;
1459 if (
MI->getOpcode() == BPF::LD_imm64) {
1474 processGlobalValue(
MI->getOperand(1));
1475 }
else if (
MI->getOpcode() == BPF::CORE_LD64 ||
1476 MI->getOpcode() == BPF::CORE_LD32 ||
1477 MI->getOpcode() == BPF::CORE_ST ||
1478 MI->getOpcode() == BPF::CORE_SHIFT) {
1480 processGlobalValue(
MI->getOperand(3));
1481 }
else if (
MI->getOpcode() == BPF::JAL) {
1498 if (LineInfoGenerated ==
false) {
1499 auto *S =
MI->getMF()->getFunction().getSubprogram();
1503 constructLineInfo(FuncLabel, S->getFile(), S->getLine(), 0);
1504 LineInfoGenerated =
true;
1511 MCSymbol *LineSym = OS.getContext().createTempSymbol();
1512 OS.emitLabel(LineSym);
1515 constructLineInfo(LineSym,
DL->getFile(),
DL.getLine(),
DL.getCol());
1517 LineInfoGenerated =
true;
1521void BTFDebug::processGlobals(
bool ProcessingMapDef) {
1527 std::optional<SectionKind> GVKind;
1529 if (!
Global.isDeclarationForLinker())
1532 if (
Global.isDeclarationForLinker())
1533 SecName =
Global.hasSection() ?
Global.getSection() :
"";
1534 else if (GVKind->isCommon())
1542 if (ProcessingMapDef != SecName.
starts_with(
".maps"))
1548 if (SecName ==
".rodata" &&
Global.hasPrivateLinkage() &&
1549 DataSecEntries.find(SecName) == DataSecEntries.end()) {
1551 if (!GVKind->isMergeableCString() && !GVKind->isMergeableConst()) {
1552 DataSecEntries[std::string(SecName)] =
1553 std::make_unique<BTFKindDataSec>(
Asm, std::string(SecName));
1558 Global.getDebugInfo(GVs);
1561 if (GVs.
size() == 0)
1564 uint32_t GVTypeId = 0;
1565 DIGlobalVariable *DIGlobal =
nullptr;
1566 for (
auto *GVE : GVs) {
1567 DIGlobal = GVE->getVariable();
1569 visitMapDefType(DIGlobal->
getType(), GVTypeId);
1572 visitTypeEntry(Ty, GVTypeId,
false,
false);
1595 }
else if (
Global.hasInitializer()) {
1602 std::make_unique<BTFKindVar>(
Global.getName(), GVTypeId, GVarInfo);
1603 uint32_t VarId = addType(std::move(VarEntry));
1608 if (SecName.
empty())
1612 auto [It,
Inserted] = DataSecEntries.try_emplace(std::string(SecName));
1614 It->second = std::make_unique<BTFKindDataSec>(
Asm, std::string(SecName));
1617 const DataLayout &
DL =
Global.getDataLayout();
1618 uint32_t
Size =
DL.getTypeAllocSize(
Global.getValueType());
1620 It->second->addDataSecEntry(VarId,
Asm->getSymbol(&
Global),
Size);
1622 if (
Global.hasInitializer())
1623 processGlobalInitializer(
Global.getInitializer());
1638void BTFDebug::processGlobalInitializer(
const Constant *
C) {
1640 processFuncPrototypes(Fn);
1642 for (
unsigned I = 0,
N = CA->getNumOperands();
I <
N; ++
I)
1643 processGlobalInitializer(CA->getOperand(
I));
1649 if (
MI->getOpcode() == BPF::LD_imm64) {
1660 auto [Imm,
Reloc] = PatchImms[GVar];
1671 }
else if (
MI->getOpcode() == BPF::CORE_LD64 ||
1672 MI->getOpcode() == BPF::CORE_LD32 ||
1673 MI->getOpcode() == BPF::CORE_ST ||
1674 MI->getOpcode() == BPF::CORE_SHIFT) {
1680 uint32_t Imm = PatchImms[GVar].first;
1682 if (
MI->getOperand(0).isImm())
1695void BTFDebug::processFuncPrototypes(
const Function *
F) {
1700 if (!SP || SP->isDefinition())
1704 if (!ProtoFunctions.insert(
F).second)
1708 const std::unordered_map<uint32_t, StringRef> FuncArgNames;
1709 visitSubroutineType(SP->getType(),
false, FuncArgNames, ProtoTypeId);
1712 if (
F->hasSection()) {
1715 auto [It, Inserted] = DataSecEntries.try_emplace(std::string(SecName));
1717 It->second = std::make_unique<BTFKindDataSec>(
Asm, std::string(SecName));
1726 if (MapDefNotCollected) {
1727 processGlobals(
true);
1728 MapDefNotCollected =
false;
1732 processGlobals(
false);
1738 processFuncPrototypes(&
F);
1741 for (
auto &DataSec : DataSecEntries)
1742 addType(std::move(DataSec.second));
1745 for (
auto &
Fixup : FixupDerivedTypes) {
1748 bool IsUnion = CTy->
getTag() == dwarf::DW_TAG_union_type;
1759 if (StructTypeId == 0) {
1760 auto FwdTypeEntry = std::make_unique<BTFTypeFwd>(TypeName, IsUnion);
1761 StructTypeId = addType(std::move(FwdTypeEntry));
1764 for (
auto &TypeInfo :
Fixup.second) {
1768 int TmpTypeId = genBTFTypeTags(DTy, StructTypeId);
1777 for (
const auto &TypeEntry : TypeEntries)
1778 TypeEntry->completeType(*
this);
1782 emitBTFExtSection();
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static const char * BTFKindStr[]
static const DIType * tryRemoveAtomicType(const DIType *Ty)
This file contains support for writing BTF debug info.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
This file contains constants used for implementing Dwarf debug support.
Module.h This file contains the declarations for the Module class.
PowerPC TLS Dynamic Call Fixup
static StringRef getName(Value *V)
static enum BaseType getBaseType(const Value *Val)
Return the baseType for Val which states whether Val is exclusively derived from constant/null,...
Annotations lets you mark points and ranges inside source code, for tests:
This class is intended to be used as a driving class for all asm writers.
MCSymbol * getSymbol(const GlobalValue *GV) const
TargetMachine & TM
Target machine description.
static constexpr StringRef TypeIdAttr
The attribute attached to globals representing a type id.
static constexpr StringRef AmaAttr
The attribute attached to globals representing a field access.
Collect and emit BTF information.
void endFunctionImpl(const MachineFunction *MF) override
Post process after all instructions in this function are processed.
void beginInstruction(const MachineInstr *MI) override
Process beginning of an instruction.
bool InstLower(const MachineInstr *MI, MCInst &OutMI)
Emit proper patchable instructions.
size_t addString(StringRef S)
Add string to the string table.
uint32_t getArrayIndexTypeId()
Get the special array index type id.
uint32_t getTypeId(const DIType *Ty)
Get the type id for a particular DIType.
void endModule() override
Complete all the types and emit the BTF sections.
void beginFunctionImpl(const MachineFunction *MF) override
Gather pre-function debug information.
void emitType(MCStreamer &OS) override
Emit types for this BTF type entry.
void completeType(BTFDebug &BDebug) override
Complete BTF type generation after all related DebugInfo types have been visited so their BTF type id...
BTFKindDataSec(AsmPrinter *AsmPrt, std::string SecName)
BTFKindVar(StringRef VarName, uint32_t TypeId, uint32_t VarInfo)
void emitType(MCStreamer &OS) override
Emit types for this BTF type entry.
void completeType(BTFDebug &BDebug) override
Complete BTF type generation after all related DebugInfo types have been visited so their BTF type id...
uint32_t addString(StringRef S)
Add a string to the string table and returns its offset in the table.
BTFTypeArray(uint32_t ElemTypeId, uint32_t NumElems)
void emitType(MCStreamer &OS) override
Emit types for this BTF type entry.
void completeType(BTFDebug &BDebug) override
Represent a BTF array.
struct BTF::CommonType BTFType
virtual void emitType(MCStreamer &OS)
Emit types for this BTF type entry.
uint32_t roundupToBytes(uint32_t NumBits)
void completeType(BTFDebug &BDebug) override
Complete BTF type generation after all related DebugInfo types have been visited so their BTF type id...
void emitType(MCStreamer &OS) override
Emit types for this BTF type entry.
BTFTypeDeclTag(uint32_t BaseTypeId, int ComponentId, StringRef Tag)
Handle several derived types include pointer, const, volatile, typedef and restrict.
void completeType(BTFDebug &BDebug) override
Complete BTF type generation after all related DebugInfo types have been visited so their BTF type id...
void emitType(MCStreamer &OS) override
Emit types for this BTF type entry.
void setPointeeType(uint32_t PointeeType)
BTFTypeDerived(const DIDerivedType *Ty, unsigned Tag, bool NeedsFixup)
void completeType(BTFDebug &BDebug) override
Complete BTF type generation after all related DebugInfo types have been visited so their BTF type id...
BTFTypeEnum64(const DICompositeType *ETy, uint32_t NumValues, bool IsSigned)
void emitType(MCStreamer &OS) override
Emit types for this BTF type entry.
void completeType(BTFDebug &BDebug) override
Complete BTF type generation after all related DebugInfo types have been visited so their BTF type id...
BTFTypeEnum(const DICompositeType *ETy, uint32_t NumValues, bool IsSigned)
void emitType(MCStreamer &OS) override
Emit types for this BTF type entry.
void completeType(BTFDebug &BDebug) override
Complete BTF type generation after all related DebugInfo types have been visited so their BTF type id...
BTFTypeFloat(uint32_t SizeInBits, StringRef TypeName)
BTFTypeFuncProto(const DISubroutineType *STy, uint32_t NumParams, const std::unordered_map< uint32_t, StringRef > &FuncArgNames)
The Func kind represents both subprogram and pointee of function pointers.
void completeType(BTFDebug &BDebug) override
Complete BTF type generation after all related DebugInfo types have been visited so their BTF type id...
void emitType(MCStreamer &OS) override
Emit types for this BTF type entry.
void emitType(MCStreamer &OS) override
Emit types for this BTF type entry.
BTFTypeFunc(StringRef FuncName, uint32_t ProtoTypeId, uint32_t Scope)
void completeType(BTFDebug &BDebug) override
Complete BTF type generation after all related DebugInfo types have been visited so their BTF type id...
void emitType(MCStreamer &OS) override
Emit types for this BTF type entry.
BTFTypeFwd(StringRef Name, bool IsUnion)
Represent a struct/union forward declaration.
void completeType(BTFDebug &BDebug) override
Complete BTF type generation after all related DebugInfo types have been visited so their BTF type id...
void emitType(MCStreamer &OS) override
Emit types for this BTF type entry.
BTFTypeInt(uint32_t Encoding, uint32_t SizeInBits, uint32_t OffsetInBits, StringRef TypeName)
void completeType(BTFDebug &BDebug) override
Complete BTF type generation after all related DebugInfo types have been visited so their BTF type id...
void emitType(MCStreamer &OS) override
Emit types for this BTF type entry.
BTFTypeStruct(const DICompositeType *STy, bool IsStruct, bool HasBitField, uint32_t NumMembers)
Represent either a struct or a union.
void completeType(BTFDebug &BDebug) override
Complete BTF type generation after all related DebugInfo types have been visited so their BTF type id...
void completeType(BTFDebug &BDebug) override
Complete BTF type generation after all related DebugInfo types have been visited so their BTF type id...
BTFTypeTypeTag(uint32_t NextTypeId, StringRef Tag)
This is an important base class in LLVM.
Basic type, like 'int' or 'float'.
unsigned getEncoding() const
DIDerivedType * getDiscriminator() const
DINodeArray getElements() const
DINodeArray getAnnotations() const
DIType * getBaseType() const
DINodeArray getAnnotations() const
Get annotations associated with this derived type.
DINodeArray getAnnotations() const
Tagged DWARF-like metadata node.
LLVM_ABI dwarf::Tag getTag() const
Subprogram description. Uses SubclassData1.
LLVM_ABI BoundType getCount() const
Type array for a subprogram.
DITypeRefArray getTypeArray() const
uint64_t getOffsetInBits() const
StringRef getName() const
bool isForwardDecl() const
uint64_t getSizeInBits() const
const MachineInstr * CurMI
If nonnull, stores the current machine instruction we're processing.
AsmPrinter * Asm
Target of debug info emission.
MachineModuleInfo * MMI
Collected machine module information.
DebugLoc PrevInstLoc
Previous instruction's location information.
DebugHandlerBase(AsmPrinter *A)
void beginInstruction(const MachineInstr *MI) override
Process beginning of an instruction.
DISubprogram * getSubprogram() const
Get the attached subprogram.
MDNode * getMetadata(unsigned KindID) const
Get the current metadata attachments for the given kind, if any.
@ InternalLinkage
Rename collisions when linking (static functions).
@ WeakODRLinkage
Same, but only replaced by something equivalent.
@ ExternalLinkage
Externally visible function.
@ WeakAnyLinkage
Keep one copy of named function when linking (weak)
@ ExternalWeakLinkage
ExternalWeak linkage description.
bool hasAttribute(Attribute::AttrKind Kind) const
Return true if the attribute exists.
MCSectionELF * getELFSection(const Twine &Section, unsigned Type, unsigned Flags)
Instances of this class represent a single low-level machine instruction.
void addOperand(const MCOperand Op)
void setOpcode(unsigned Op)
static MCOperand createReg(MCRegister Reg)
static MCOperand createImm(int64_t Val)
This represents a section on linux, lots of unix variants and some bare metal systems.
Instances of this class represent a uniqued identifier for a section in the current translation unit.
void setAlignment(Align Value)
StringRef getName() const
Streaming machine code generation interface.
virtual void AddComment(const Twine &T, bool EOL=true)
Add a textual comment.
void emitInt32(uint64_t Value)
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
bool isInSection() const
isInSection - Check if this symbol is defined in some section (i.e., it is defined but not absolute).
MCSection & getSection() const
Get the section associated with a defined, non-absolute symbol.
const MDOperand & getOperand(unsigned I) const
Function & getFunction()
Return the LLVM function that this machine code represents.
Representation of each machine instruction.
const Module * getModule() const
MachineOperand class - Representation of each machine instruction operand.
const GlobalValue * getGlobal() const
bool isReg() const
isReg - Tests if this is a MO_Register operand.
bool isGlobal() const
isGlobal - Tests if this is a MO_GlobalAddress operand.
const char * getSymbolName() const
static std::unique_ptr< MemoryBuffer > getMemBufferCopy(StringRef InputData, const Twine &BufferName="")
Open the specified memory range as a MemoryBuffer, copying the contents and taking ownership of it.
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFile(const Twine &Filename, bool IsText=false, bool RequiresNullTerminator=true, bool IsVolatile=false, std::optional< Align > Alignment=std::nullopt)
Open the specified file as a MemoryBuffer, returning a new MemoryBuffer if successful,...
A Module instance is used to store all the information related to an LLVM module.
void push_back(const T &Elt)
StringRef - Represent a constant reference to a string, i.e.
constexpr StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
bool starts_with(StringRef Prefix) const
Check if this string starts with the given Prefix.
constexpr bool empty() const
empty - Check if the string is empty.
constexpr size_t size() const
size - Get the string size.
size_t find_first_of(char C, size_t From=0) const
Find the first character in the string that is C, or npos if not found.
Class to represent struct types.
LLVM_ABI StringRef getName() const
Return the name for this struct type if it has an identity.
static SectionKind getKindForGlobal(const GlobalObject *GO, const TargetMachine &TM)
Classify the specified global variable into a set of target independent categories embodied in Sectio...
MCSection * SectionForGlobal(const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const
This method computes the appropriate section to emit the specified global variable or function defini...
virtual TargetLoweringObjectFile * getObjFileLowering() const
static Twine utohexstr(uint64_t Val)
LLVM Value Representation.
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
@ VAR_GLOBAL_ALLOCATED
Linkage: ExternalLinkage.
@ VAR_STATIC
Linkage: InternalLinkage.
@ VAR_GLOBAL_EXTERNAL
Linkage: ExternalLinkage.
@ MAX_VLEN
Max # of struct/union/enum members or func args.
@ C
The default llvm calling convention, compatible with C.
StringMapEntry< std::atomic< TypeEntryBody * > > TypeEntry
ScopedSetting scopedDisable()
This is an optimization pass for GlobalISel generic memory operations.
FunctionAddr VTableAddr Value
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
auto dyn_cast_or_null(const Y &Val)
FunctionAddr VTableAddr Count
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
@ Global
Append to llvm.global_dtors.
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Represent one field relocation.
uint32_t RelocKind
What to patch the instruction.
const MCSymbol * Label
MCSymbol identifying insn for the reloc.
uint32_t OffsetNameOff
The string to traverse types.
Represent one func and its type id.
uint32_t TypeId
Type id referring to .BTF type section.
const MCSymbol * Label
Func MCSymbol.
uint32_t LineOff
line offset in the .BTF string table
MCSymbol * Label
MCSymbol identifying insn for the lineinfo.
uint32_t ColumnNum
the column number
uint32_t FileNameOff
file name offset in the .BTF string table
uint32_t LineNum
the line number
BTF_KIND_ENUM64 is followed by multiple "struct BTFEnum64".
uint32_t NameOff
Enum name offset in the string table.
uint32_t Val_Hi32
Enum member hi32 value.
uint32_t Val_Lo32
Enum member lo32 value.
BTF_KIND_ENUM is followed by multiple "struct BTFEnum".
int32_t Val
Enum member value.
uint32_t NameOff
Enum name offset in the string table.
BTF_KIND_STRUCT and BTF_KIND_UNION are followed by multiple "struct BTFMember".
uint32_t NameOff
Member name offset in the string table.
uint32_t Offset
BitOffset or BitFieldSize+BitOffset.
uint32_t Type
Member type.
BTF_KIND_FUNC_PROTO are followed by multiple "struct BTFParam".