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