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 "llvm/BinaryFormat/ELF.h"
15 #include "llvm/CodeGen/AsmPrinter.h"
16 #include "llvm/CodeGen/MachineModuleInfo.h"
17 #include "llvm/MC/MCContext.h"
18 #include "llvm/MC/MCObjectFileInfo.h"
19 #include "llvm/MC/MCSectionELF.h"
20 #include "llvm/MC/MCStreamer.h"
21 #include <fstream>
22 #include <sstream>
23 
24 using namespace llvm;
25 
26 static const char *BTFKindStr[] = {
27 #define HANDLE_BTF_KIND(ID, NAME) "BTF_KIND_" #NAME,
28 #include "BTF.def"
29 };
30 
31 /// Emit a BTF common type.
32 void BTFTypeBase::emitType(MCStreamer &OS) {
33   OS.AddComment(std::string(BTFKindStr[Kind]) + "(id = " + std::to_string(Id) +
34                 ")");
35   OS.EmitIntValue(BTFType.NameOff, 4);
36   OS.AddComment("0x" + Twine::utohexstr(BTFType.Info));
37   OS.EmitIntValue(BTFType.Info, 4);
38   OS.EmitIntValue(BTFType.Size, 4);
39 }
40 
41 BTFTypeDerived::BTFTypeDerived(const DIDerivedType *DTy, unsigned Tag)
42     : DTy(DTy) {
43   switch (Tag) {
44   case dwarf::DW_TAG_pointer_type:
45     Kind = BTF::BTF_KIND_PTR;
46     break;
47   case dwarf::DW_TAG_const_type:
48     Kind = BTF::BTF_KIND_CONST;
49     break;
50   case dwarf::DW_TAG_volatile_type:
51     Kind = BTF::BTF_KIND_VOLATILE;
52     break;
53   case dwarf::DW_TAG_typedef:
54     Kind = BTF::BTF_KIND_TYPEDEF;
55     break;
56   case dwarf::DW_TAG_restrict_type:
57     Kind = BTF::BTF_KIND_RESTRICT;
58     break;
59   default:
60     llvm_unreachable("Unknown DIDerivedType Tag");
61   }
62   BTFType.Info = Kind << 24;
63 }
64 
65 void BTFTypeDerived::completeType(BTFDebug &BDebug) {
66   BTFType.NameOff = BDebug.addString(DTy->getName());
67 
68   // The base type for PTR/CONST/VOLATILE could be void.
69   const DIType *ResolvedType = DTy->getBaseType().resolve();
70   if (!ResolvedType) {
71     assert((Kind == BTF::BTF_KIND_PTR || Kind == BTF::BTF_KIND_CONST ||
72             Kind == BTF::BTF_KIND_VOLATILE) &&
73            "Invalid null basetype");
74     BTFType.Type = 0;
75   } else {
76     BTFType.Type = BDebug.getTypeId(ResolvedType);
77   }
78 }
79 
80 void BTFTypeDerived::emitType(MCStreamer &OS) { BTFTypeBase::emitType(OS); }
81 
82 /// Represent a struct/union forward declaration.
83 BTFTypeFwd::BTFTypeFwd(StringRef Name, bool IsUnion) : Name(Name) {
84   Kind = BTF::BTF_KIND_FWD;
85   BTFType.Info = IsUnion << 31 | Kind << 24;
86   BTFType.Type = 0;
87 }
88 
89 void BTFTypeFwd::completeType(BTFDebug &BDebug) {
90   BTFType.NameOff = BDebug.addString(Name);
91 }
92 
93 void BTFTypeFwd::emitType(MCStreamer &OS) { BTFTypeBase::emitType(OS); }
94 
95 BTFTypeInt::BTFTypeInt(uint32_t Encoding, uint32_t SizeInBits,
96                        uint32_t OffsetInBits, StringRef TypeName)
97     : Name(TypeName) {
98   // Translate IR int encoding to BTF int encoding.
99   uint8_t BTFEncoding;
100   switch (Encoding) {
101   case dwarf::DW_ATE_boolean:
102     BTFEncoding = BTF::INT_BOOL;
103     break;
104   case dwarf::DW_ATE_signed:
105   case dwarf::DW_ATE_signed_char:
106     BTFEncoding = BTF::INT_SIGNED;
107     break;
108   case dwarf::DW_ATE_unsigned:
109   case dwarf::DW_ATE_unsigned_char:
110     BTFEncoding = 0;
111     break;
112   default:
113     llvm_unreachable("Unknown BTFTypeInt Encoding");
114   }
115 
116   Kind = BTF::BTF_KIND_INT;
117   BTFType.Info = Kind << 24;
118   BTFType.Size = roundupToBytes(SizeInBits);
119   IntVal = (BTFEncoding << 24) | OffsetInBits << 16 | SizeInBits;
120 }
121 
122 void BTFTypeInt::completeType(BTFDebug &BDebug) {
123   BTFType.NameOff = BDebug.addString(Name);
124 }
125 
126 void BTFTypeInt::emitType(MCStreamer &OS) {
127   BTFTypeBase::emitType(OS);
128   OS.AddComment("0x" + Twine::utohexstr(IntVal));
129   OS.EmitIntValue(IntVal, 4);
130 }
131 
132 BTFTypeEnum::BTFTypeEnum(const DICompositeType *ETy, uint32_t VLen) : ETy(ETy) {
133   Kind = BTF::BTF_KIND_ENUM;
134   BTFType.Info = Kind << 24 | VLen;
135   BTFType.Size = roundupToBytes(ETy->getSizeInBits());
136 }
137 
138 void BTFTypeEnum::completeType(BTFDebug &BDebug) {
139   BTFType.NameOff = BDebug.addString(ETy->getName());
140 
141   DINodeArray Elements = ETy->getElements();
142   for (const auto Element : Elements) {
143     const auto *Enum = cast<DIEnumerator>(Element);
144 
145     struct BTF::BTFEnum BTFEnum;
146     BTFEnum.NameOff = BDebug.addString(Enum->getName());
147     // BTF enum value is 32bit, enforce it.
148     BTFEnum.Val = static_cast<uint32_t>(Enum->getValue());
149     EnumValues.push_back(BTFEnum);
150   }
151 }
152 
153 void BTFTypeEnum::emitType(MCStreamer &OS) {
154   BTFTypeBase::emitType(OS);
155   for (const auto &Enum : EnumValues) {
156     OS.EmitIntValue(Enum.NameOff, 4);
157     OS.EmitIntValue(Enum.Val, 4);
158   }
159 }
160 
161 BTFTypeArray::BTFTypeArray(uint32_t ElemTypeId, uint32_t NumElems) {
162   Kind = BTF::BTF_KIND_ARRAY;
163   BTFType.NameOff = 0;
164   BTFType.Info = Kind << 24;
165   BTFType.Size = 0;
166 
167   ArrayInfo.ElemType = ElemTypeId;
168   ArrayInfo.Nelems = NumElems;
169 }
170 
171 /// Represent a BTF array.
172 void BTFTypeArray::completeType(BTFDebug &BDebug) {
173 
174   // The IR does not really have a type for the index.
175   // A special type for array index should have been
176   // created during initial type traversal. Just
177   // retrieve that type id.
178   ArrayInfo.IndexType = BDebug.getArrayIndexTypeId();
179 }
180 
181 void BTFTypeArray::emitType(MCStreamer &OS) {
182   BTFTypeBase::emitType(OS);
183   OS.EmitIntValue(ArrayInfo.ElemType, 4);
184   OS.EmitIntValue(ArrayInfo.IndexType, 4);
185   OS.EmitIntValue(ArrayInfo.Nelems, 4);
186 }
187 
188 /// Represent either a struct or a union.
189 BTFTypeStruct::BTFTypeStruct(const DICompositeType *STy, bool IsStruct,
190                              bool HasBitField, uint32_t Vlen)
191     : STy(STy), HasBitField(HasBitField) {
192   Kind = IsStruct ? BTF::BTF_KIND_STRUCT : BTF::BTF_KIND_UNION;
193   BTFType.Size = roundupToBytes(STy->getSizeInBits());
194   BTFType.Info = (HasBitField << 31) | (Kind << 24) | Vlen;
195 }
196 
197 void BTFTypeStruct::completeType(BTFDebug &BDebug) {
198   BTFType.NameOff = BDebug.addString(STy->getName());
199 
200   // Add struct/union members.
201   const DINodeArray Elements = STy->getElements();
202   for (const auto *Element : Elements) {
203     struct BTF::BTFMember BTFMember;
204     const auto *DDTy = cast<DIDerivedType>(Element);
205 
206     BTFMember.NameOff = BDebug.addString(DDTy->getName());
207     if (HasBitField) {
208       uint8_t BitFieldSize = DDTy->isBitField() ? DDTy->getSizeInBits() : 0;
209       BTFMember.Offset = BitFieldSize << 24 | DDTy->getOffsetInBits();
210     } else {
211       BTFMember.Offset = DDTy->getOffsetInBits();
212     }
213     BTFMember.Type = BDebug.getTypeId(DDTy->getBaseType().resolve());
214     Members.push_back(BTFMember);
215   }
216 }
217 
218 void BTFTypeStruct::emitType(MCStreamer &OS) {
219   BTFTypeBase::emitType(OS);
220   for (const auto &Member : Members) {
221     OS.EmitIntValue(Member.NameOff, 4);
222     OS.EmitIntValue(Member.Type, 4);
223     OS.AddComment("0x" + Twine::utohexstr(Member.Offset));
224     OS.EmitIntValue(Member.Offset, 4);
225   }
226 }
227 
228 /// The Func kind represents both subprogram and pointee of function
229 /// pointers. If the FuncName is empty, it represents a pointee of function
230 /// pointer. Otherwise, it represents a subprogram. The func arg names
231 /// are empty for pointee of function pointer case, and are valid names
232 /// for subprogram.
233 BTFTypeFuncProto::BTFTypeFuncProto(
234     const DISubroutineType *STy, uint32_t VLen,
235     const std::unordered_map<uint32_t, StringRef> &FuncArgNames)
236     : STy(STy), FuncArgNames(FuncArgNames) {
237   Kind = BTF::BTF_KIND_FUNC_PROTO;
238   BTFType.Info = (Kind << 24) | VLen;
239 }
240 
241 void BTFTypeFuncProto::completeType(BTFDebug &BDebug) {
242   DITypeRefArray Elements = STy->getTypeArray();
243   auto RetType = Elements[0].resolve();
244   BTFType.Type = RetType ? BDebug.getTypeId(RetType) : 0;
245   BTFType.NameOff = 0;
246 
247   // For null parameter which is typically the last one
248   // to represent the vararg, encode the NameOff/Type to be 0.
249   for (unsigned I = 1, N = Elements.size(); I < N; ++I) {
250     struct BTF::BTFParam Param;
251     auto Element = Elements[I].resolve();
252     if (Element) {
253       Param.NameOff = BDebug.addString(FuncArgNames[I]);
254       Param.Type = BDebug.getTypeId(Element);
255     } else {
256       Param.NameOff = 0;
257       Param.Type = 0;
258     }
259     Parameters.push_back(Param);
260   }
261 }
262 
263 void BTFTypeFuncProto::emitType(MCStreamer &OS) {
264   BTFTypeBase::emitType(OS);
265   for (const auto &Param : Parameters) {
266     OS.EmitIntValue(Param.NameOff, 4);
267     OS.EmitIntValue(Param.Type, 4);
268   }
269 }
270 
271 BTFTypeFunc::BTFTypeFunc(StringRef FuncName, uint32_t ProtoTypeId)
272     : Name(FuncName) {
273   Kind = BTF::BTF_KIND_FUNC;
274   BTFType.Info = Kind << 24;
275   BTFType.Type = ProtoTypeId;
276 }
277 
278 void BTFTypeFunc::completeType(BTFDebug &BDebug) {
279   BTFType.NameOff = BDebug.addString(Name);
280 }
281 
282 void BTFTypeFunc::emitType(MCStreamer &OS) { BTFTypeBase::emitType(OS); }
283 
284 BTFKindVar::BTFKindVar(StringRef VarName, uint32_t TypeId, uint32_t VarInfo)
285     : Name(VarName) {
286   Kind = BTF::BTF_KIND_VAR;
287   BTFType.Info = Kind << 24;
288   BTFType.Type = TypeId;
289   Info = VarInfo;
290 }
291 
292 void BTFKindVar::completeType(BTFDebug &BDebug) {
293   BTFType.NameOff = BDebug.addString(Name);
294 }
295 
296 void BTFKindVar::emitType(MCStreamer &OS) {
297   BTFTypeBase::emitType(OS);
298   OS.EmitIntValue(Info, 4);
299 }
300 
301 BTFKindDataSec::BTFKindDataSec(AsmPrinter *AsmPrt, std::string SecName)
302     : Asm(AsmPrt), Name(SecName) {
303   Kind = BTF::BTF_KIND_DATASEC;
304   BTFType.Info = Kind << 24;
305   BTFType.Size = 0;
306 }
307 
308 void BTFKindDataSec::completeType(BTFDebug &BDebug) {
309   BTFType.NameOff = BDebug.addString(Name);
310   BTFType.Info |= Vars.size();
311 }
312 
313 void BTFKindDataSec::emitType(MCStreamer &OS) {
314   BTFTypeBase::emitType(OS);
315 
316   for (const auto &V : Vars) {
317     OS.EmitIntValue(std::get<0>(V), 4);
318     Asm->EmitLabelReference(std::get<1>(V), 4);
319     OS.EmitIntValue(std::get<2>(V), 4);
320   }
321 }
322 
323 uint32_t BTFStringTable::addString(StringRef S) {
324   // Check whether the string already exists.
325   for (auto &OffsetM : OffsetToIdMap) {
326     if (Table[OffsetM.second] == S)
327       return OffsetM.first;
328   }
329   // Not find, add to the string table.
330   uint32_t Offset = Size;
331   OffsetToIdMap[Offset] = Table.size();
332   Table.push_back(S);
333   Size += S.size() + 1;
334   return Offset;
335 }
336 
337 BTFDebug::BTFDebug(AsmPrinter *AP)
338     : DebugHandlerBase(AP), OS(*Asm->OutStreamer), SkipInstruction(false),
339       LineInfoGenerated(false), SecNameOff(0), ArrayIndexTypeId(0) {
340   addString("\0");
341 }
342 
343 uint32_t BTFDebug::addType(std::unique_ptr<BTFTypeBase> TypeEntry,
344                            const DIType *Ty) {
345   TypeEntry->setId(TypeEntries.size() + 1);
346   uint32_t Id = TypeEntry->getId();
347   DIToIdMap[Ty] = Id;
348   TypeEntries.push_back(std::move(TypeEntry));
349   return Id;
350 }
351 
352 uint32_t BTFDebug::addType(std::unique_ptr<BTFTypeBase> TypeEntry) {
353   TypeEntry->setId(TypeEntries.size() + 1);
354   uint32_t Id = TypeEntry->getId();
355   TypeEntries.push_back(std::move(TypeEntry));
356   return Id;
357 }
358 
359 void BTFDebug::visitBasicType(const DIBasicType *BTy, uint32_t &TypeId) {
360   // Only int types are supported in BTF.
361   uint32_t Encoding = BTy->getEncoding();
362   if (Encoding != dwarf::DW_ATE_boolean && Encoding != dwarf::DW_ATE_signed &&
363       Encoding != dwarf::DW_ATE_signed_char &&
364       Encoding != dwarf::DW_ATE_unsigned &&
365       Encoding != dwarf::DW_ATE_unsigned_char)
366     return;
367 
368   // Create a BTF type instance for this DIBasicType and put it into
369   // DIToIdMap for cross-type reference check.
370   auto TypeEntry = llvm::make_unique<BTFTypeInt>(
371       Encoding, BTy->getSizeInBits(), BTy->getOffsetInBits(), BTy->getName());
372   TypeId = addType(std::move(TypeEntry), BTy);
373 }
374 
375 /// Handle subprogram or subroutine types.
376 void BTFDebug::visitSubroutineType(
377     const DISubroutineType *STy, bool ForSubprog,
378     const std::unordered_map<uint32_t, StringRef> &FuncArgNames,
379     uint32_t &TypeId) {
380   DITypeRefArray Elements = STy->getTypeArray();
381   uint32_t VLen = Elements.size() - 1;
382   if (VLen > BTF::MAX_VLEN)
383     return;
384 
385   // Subprogram has a valid non-zero-length name, and the pointee of
386   // a function pointer has an empty name. The subprogram type will
387   // not be added to DIToIdMap as it should not be referenced by
388   // any other types.
389   auto TypeEntry = llvm::make_unique<BTFTypeFuncProto>(STy, VLen, FuncArgNames);
390   if (ForSubprog)
391     TypeId = addType(std::move(TypeEntry)); // For subprogram
392   else
393     TypeId = addType(std::move(TypeEntry), STy); // For func ptr
394 
395   // Visit return type and func arg types.
396   for (const auto Element : Elements) {
397     visitTypeEntry(Element.resolve());
398   }
399 }
400 
401 /// Handle structure/union types.
402 void BTFDebug::visitStructType(const DICompositeType *CTy, bool IsStruct,
403                                uint32_t &TypeId) {
404   const DINodeArray Elements = CTy->getElements();
405   uint32_t VLen = Elements.size();
406   if (VLen > BTF::MAX_VLEN)
407     return;
408 
409   // Check whether we have any bitfield members or not
410   bool HasBitField = false;
411   for (const auto *Element : Elements) {
412     auto E = cast<DIDerivedType>(Element);
413     if (E->isBitField()) {
414       HasBitField = true;
415       break;
416     }
417   }
418 
419   auto TypeEntry =
420       llvm::make_unique<BTFTypeStruct>(CTy, IsStruct, HasBitField, VLen);
421   TypeId = addType(std::move(TypeEntry), CTy);
422 
423   // Visit all struct members.
424   for (const auto *Element : Elements)
425     visitTypeEntry(cast<DIDerivedType>(Element));
426 }
427 
428 void BTFDebug::visitArrayType(const DICompositeType *CTy, uint32_t &TypeId) {
429   // Visit array element type.
430   uint32_t ElemTypeId;
431   visitTypeEntry(CTy->getBaseType().resolve(), ElemTypeId);
432 
433   if (!CTy->getSizeInBits()) {
434     auto TypeEntry = llvm::make_unique<BTFTypeArray>(ElemTypeId, 0);
435     ElemTypeId = addType(std::move(TypeEntry), CTy);
436   } else {
437     // Visit array dimensions.
438     DINodeArray Elements = CTy->getElements();
439     for (int I = Elements.size() - 1; I >= 0; --I) {
440       if (auto *Element = dyn_cast_or_null<DINode>(Elements[I]))
441         if (Element->getTag() == dwarf::DW_TAG_subrange_type) {
442           const DISubrange *SR = cast<DISubrange>(Element);
443           auto *CI = SR->getCount().dyn_cast<ConstantInt *>();
444           int64_t Count = CI->getSExtValue();
445 
446           auto TypeEntry = llvm::make_unique<BTFTypeArray>(ElemTypeId, Count);
447           if (I == 0)
448             ElemTypeId = addType(std::move(TypeEntry), CTy);
449           else
450             ElemTypeId = addType(std::move(TypeEntry));
451         }
452     }
453   }
454 
455   // The array TypeId is the type id of the outermost dimension.
456   TypeId = ElemTypeId;
457 
458   // The IR does not have a type for array index while BTF wants one.
459   // So create an array index type if there is none.
460   if (!ArrayIndexTypeId) {
461     auto TypeEntry = llvm::make_unique<BTFTypeInt>(dwarf::DW_ATE_unsigned, 32,
462                                                    0, "__ARRAY_SIZE_TYPE__");
463     ArrayIndexTypeId = addType(std::move(TypeEntry));
464   }
465 }
466 
467 void BTFDebug::visitEnumType(const DICompositeType *CTy, uint32_t &TypeId) {
468   DINodeArray Elements = CTy->getElements();
469   uint32_t VLen = Elements.size();
470   if (VLen > BTF::MAX_VLEN)
471     return;
472 
473   auto TypeEntry = llvm::make_unique<BTFTypeEnum>(CTy, VLen);
474   TypeId = addType(std::move(TypeEntry), CTy);
475   // No need to visit base type as BTF does not encode it.
476 }
477 
478 /// Handle structure/union forward declarations.
479 void BTFDebug::visitFwdDeclType(const DICompositeType *CTy, bool IsUnion,
480                                 uint32_t &TypeId) {
481   auto TypeEntry = llvm::make_unique<BTFTypeFwd>(CTy->getName(), IsUnion);
482   TypeId = addType(std::move(TypeEntry), CTy);
483 }
484 
485 /// Handle structure, union, array and enumeration types.
486 void BTFDebug::visitCompositeType(const DICompositeType *CTy,
487                                   uint32_t &TypeId) {
488   auto Tag = CTy->getTag();
489   if (Tag == dwarf::DW_TAG_structure_type || Tag == dwarf::DW_TAG_union_type) {
490     // Handle forward declaration differently as it does not have members.
491     if (CTy->isForwardDecl())
492       visitFwdDeclType(CTy, Tag == dwarf::DW_TAG_union_type, TypeId);
493     else
494       visitStructType(CTy, Tag == dwarf::DW_TAG_structure_type, TypeId);
495   } else if (Tag == dwarf::DW_TAG_array_type)
496     visitArrayType(CTy, TypeId);
497   else if (Tag == dwarf::DW_TAG_enumeration_type)
498     visitEnumType(CTy, TypeId);
499 }
500 
501 /// Handle pointer, typedef, const, volatile, restrict and member types.
502 void BTFDebug::visitDerivedType(const DIDerivedType *DTy, uint32_t &TypeId) {
503   unsigned Tag = DTy->getTag();
504 
505   if (Tag == dwarf::DW_TAG_pointer_type || Tag == dwarf::DW_TAG_typedef ||
506       Tag == dwarf::DW_TAG_const_type || Tag == dwarf::DW_TAG_volatile_type ||
507       Tag == dwarf::DW_TAG_restrict_type) {
508     auto TypeEntry = llvm::make_unique<BTFTypeDerived>(DTy, Tag);
509     TypeId = addType(std::move(TypeEntry), DTy);
510   } else if (Tag != dwarf::DW_TAG_member) {
511     return;
512   }
513 
514   // Visit base type of pointer, typedef, const, volatile, restrict or
515   // struct/union member.
516   uint32_t TempTypeId = 0;
517   visitTypeEntry(DTy->getBaseType().resolve(), TempTypeId);
518 }
519 
520 void BTFDebug::visitTypeEntry(const DIType *Ty, uint32_t &TypeId) {
521   if (!Ty || DIToIdMap.find(Ty) != DIToIdMap.end()) {
522     TypeId = DIToIdMap[Ty];
523     return;
524   }
525 
526   if (const auto *BTy = dyn_cast<DIBasicType>(Ty))
527     visitBasicType(BTy, TypeId);
528   else if (const auto *STy = dyn_cast<DISubroutineType>(Ty))
529     visitSubroutineType(STy, false, std::unordered_map<uint32_t, StringRef>(),
530                         TypeId);
531   else if (const auto *CTy = dyn_cast<DICompositeType>(Ty))
532     visitCompositeType(CTy, TypeId);
533   else if (const auto *DTy = dyn_cast<DIDerivedType>(Ty))
534     visitDerivedType(DTy, TypeId);
535   else
536     llvm_unreachable("Unknown DIType");
537 }
538 
539 void BTFDebug::visitTypeEntry(const DIType *Ty) {
540   uint32_t TypeId;
541   visitTypeEntry(Ty, TypeId);
542 }
543 
544 /// Read file contents from the actual file or from the source
545 std::string BTFDebug::populateFileContent(const DISubprogram *SP) {
546   auto File = SP->getFile();
547   std::string FileName;
548 
549   if (!File->getFilename().startswith("/") && File->getDirectory().size())
550     FileName = File->getDirectory().str() + "/" + File->getFilename().str();
551   else
552     FileName = File->getFilename();
553 
554   // No need to populate the contends if it has been populated!
555   if (FileContent.find(FileName) != FileContent.end())
556     return FileName;
557 
558   std::vector<std::string> Content;
559   std::string Line;
560   Content.push_back(Line); // Line 0 for empty string
561 
562   auto Source = File->getSource();
563   if (Source) {
564     std::istringstream InputString(Source.getValue());
565     while (std::getline(InputString, Line))
566       Content.push_back(Line);
567   } else {
568     std::ifstream InputFile(FileName);
569     while (std::getline(InputFile, Line))
570       Content.push_back(Line);
571   }
572 
573   FileContent[FileName] = Content;
574   return FileName;
575 }
576 
577 void BTFDebug::constructLineInfo(const DISubprogram *SP, MCSymbol *Label,
578                                  uint32_t Line, uint32_t Column) {
579   std::string FileName = populateFileContent(SP);
580   BTFLineInfo LineInfo;
581 
582   LineInfo.Label = Label;
583   LineInfo.FileNameOff = addString(FileName);
584   // If file content is not available, let LineOff = 0.
585   if (Line < FileContent[FileName].size())
586     LineInfo.LineOff = addString(FileContent[FileName][Line]);
587   else
588     LineInfo.LineOff = 0;
589   LineInfo.LineNum = Line;
590   LineInfo.ColumnNum = Column;
591   LineInfoTable[SecNameOff].push_back(LineInfo);
592 }
593 
594 void BTFDebug::emitCommonHeader() {
595   OS.AddComment("0x" + Twine::utohexstr(BTF::MAGIC));
596   OS.EmitIntValue(BTF::MAGIC, 2);
597   OS.EmitIntValue(BTF::VERSION, 1);
598   OS.EmitIntValue(0, 1);
599 }
600 
601 void BTFDebug::emitBTFSection() {
602   // Do not emit section if no types and only "" string.
603   if (!TypeEntries.size() && StringTable.getSize() == 1)
604     return;
605 
606   MCContext &Ctx = OS.getContext();
607   OS.SwitchSection(Ctx.getELFSection(".BTF", ELF::SHT_PROGBITS, 0));
608 
609   // Emit header.
610   emitCommonHeader();
611   OS.EmitIntValue(BTF::HeaderSize, 4);
612 
613   uint32_t TypeLen = 0, StrLen;
614   for (const auto &TypeEntry : TypeEntries)
615     TypeLen += TypeEntry->getSize();
616   StrLen = StringTable.getSize();
617 
618   OS.EmitIntValue(0, 4);
619   OS.EmitIntValue(TypeLen, 4);
620   OS.EmitIntValue(TypeLen, 4);
621   OS.EmitIntValue(StrLen, 4);
622 
623   // Emit type table.
624   for (const auto &TypeEntry : TypeEntries)
625     TypeEntry->emitType(OS);
626 
627   // Emit string table.
628   uint32_t StringOffset = 0;
629   for (const auto &S : StringTable.getTable()) {
630     OS.AddComment("string offset=" + std::to_string(StringOffset));
631     OS.EmitBytes(S);
632     OS.EmitBytes(StringRef("\0", 1));
633     StringOffset += S.size() + 1;
634   }
635 }
636 
637 void BTFDebug::emitBTFExtSection() {
638   // Do not emit section if empty FuncInfoTable and LineInfoTable.
639   if (!FuncInfoTable.size() && !LineInfoTable.size())
640     return;
641 
642   MCContext &Ctx = OS.getContext();
643   OS.SwitchSection(Ctx.getELFSection(".BTF.ext", ELF::SHT_PROGBITS, 0));
644 
645   // Emit header.
646   emitCommonHeader();
647   OS.EmitIntValue(BTF::ExtHeaderSize, 4);
648 
649   // Account for FuncInfo/LineInfo record size as well.
650   uint32_t FuncLen = 4, LineLen = 4;
651   for (const auto &FuncSec : FuncInfoTable) {
652     FuncLen += BTF::SecFuncInfoSize;
653     FuncLen += FuncSec.second.size() * BTF::BPFFuncInfoSize;
654   }
655   for (const auto &LineSec : LineInfoTable) {
656     LineLen += BTF::SecLineInfoSize;
657     LineLen += LineSec.second.size() * BTF::BPFLineInfoSize;
658   }
659 
660   OS.EmitIntValue(0, 4);
661   OS.EmitIntValue(FuncLen, 4);
662   OS.EmitIntValue(FuncLen, 4);
663   OS.EmitIntValue(LineLen, 4);
664 
665   // Emit func_info table.
666   OS.AddComment("FuncInfo");
667   OS.EmitIntValue(BTF::BPFFuncInfoSize, 4);
668   for (const auto &FuncSec : FuncInfoTable) {
669     OS.AddComment("FuncInfo section string offset=" +
670                   std::to_string(FuncSec.first));
671     OS.EmitIntValue(FuncSec.first, 4);
672     OS.EmitIntValue(FuncSec.second.size(), 4);
673     for (const auto &FuncInfo : FuncSec.second) {
674       Asm->EmitLabelReference(FuncInfo.Label, 4);
675       OS.EmitIntValue(FuncInfo.TypeId, 4);
676     }
677   }
678 
679   // Emit line_info table.
680   OS.AddComment("LineInfo");
681   OS.EmitIntValue(BTF::BPFLineInfoSize, 4);
682   for (const auto &LineSec : LineInfoTable) {
683     OS.AddComment("LineInfo section string offset=" +
684                   std::to_string(LineSec.first));
685     OS.EmitIntValue(LineSec.first, 4);
686     OS.EmitIntValue(LineSec.second.size(), 4);
687     for (const auto &LineInfo : LineSec.second) {
688       Asm->EmitLabelReference(LineInfo.Label, 4);
689       OS.EmitIntValue(LineInfo.FileNameOff, 4);
690       OS.EmitIntValue(LineInfo.LineOff, 4);
691       OS.AddComment("Line " + std::to_string(LineInfo.LineNum) + " Col " +
692                     std::to_string(LineInfo.ColumnNum));
693       OS.EmitIntValue(LineInfo.LineNum << 10 | LineInfo.ColumnNum, 4);
694     }
695   }
696 }
697 
698 void BTFDebug::beginFunctionImpl(const MachineFunction *MF) {
699   auto *SP = MF->getFunction().getSubprogram();
700   auto *Unit = SP->getUnit();
701 
702   if (Unit->getEmissionKind() == DICompileUnit::NoDebug) {
703     SkipInstruction = true;
704     return;
705   }
706   SkipInstruction = false;
707 
708   // Collect all types locally referenced in this function.
709   // Use RetainedNodes so we can collect all argument names
710   // even if the argument is not used.
711   std::unordered_map<uint32_t, StringRef> FuncArgNames;
712   for (const DINode *DN : SP->getRetainedNodes()) {
713     if (const auto *DV = dyn_cast<DILocalVariable>(DN)) {
714       // Collect function arguments for subprogram func type.
715       uint32_t Arg = DV->getArg();
716       if (Arg) {
717         visitTypeEntry(DV->getType().resolve());
718         FuncArgNames[Arg] = DV->getName();
719       }
720     }
721   }
722 
723   // Construct subprogram func proto type.
724   uint32_t ProtoTypeId;
725   visitSubroutineType(SP->getType(), true, FuncArgNames, ProtoTypeId);
726 
727   // Construct subprogram func type
728   auto FuncTypeEntry =
729       llvm::make_unique<BTFTypeFunc>(SP->getName(), ProtoTypeId);
730   uint32_t FuncTypeId = addType(std::move(FuncTypeEntry));
731 
732   // Construct funcinfo and the first lineinfo for the function.
733   MCSymbol *FuncLabel = Asm->getFunctionBegin();
734   BTFFuncInfo FuncInfo;
735   FuncInfo.Label = FuncLabel;
736   FuncInfo.TypeId = FuncTypeId;
737   if (FuncLabel->isInSection()) {
738     MCSection &Section = FuncLabel->getSection();
739     const MCSectionELF *SectionELF = dyn_cast<MCSectionELF>(&Section);
740     assert(SectionELF && "Null section for Function Label");
741     SecNameOff = addString(SectionELF->getSectionName());
742   } else {
743     SecNameOff = addString(".text");
744   }
745   FuncInfoTable[SecNameOff].push_back(FuncInfo);
746 }
747 
748 void BTFDebug::endFunctionImpl(const MachineFunction *MF) {
749   SkipInstruction = false;
750   LineInfoGenerated = false;
751   SecNameOff = 0;
752 }
753 
754 void BTFDebug::beginInstruction(const MachineInstr *MI) {
755   DebugHandlerBase::beginInstruction(MI);
756 
757   if (SkipInstruction || MI->isMetaInstruction() ||
758       MI->getFlag(MachineInstr::FrameSetup))
759     return;
760 
761   if (MI->isInlineAsm()) {
762     // Count the number of register definitions to find the asm string.
763     unsigned NumDefs = 0;
764     for (; MI->getOperand(NumDefs).isReg() && MI->getOperand(NumDefs).isDef();
765          ++NumDefs)
766       ;
767 
768     // Skip this inline asm instruction if the asmstr is empty.
769     const char *AsmStr = MI->getOperand(NumDefs).getSymbolName();
770     if (AsmStr[0] == 0)
771       return;
772   }
773 
774   // Skip this instruction if no DebugLoc or the DebugLoc
775   // is the same as the previous instruction.
776   const DebugLoc &DL = MI->getDebugLoc();
777   if (!DL || PrevInstLoc == DL) {
778     // This instruction will be skipped, no LineInfo has
779     // been generated, construct one based on function signature.
780     if (LineInfoGenerated == false) {
781       auto *S = MI->getMF()->getFunction().getSubprogram();
782       MCSymbol *FuncLabel = Asm->getFunctionBegin();
783       constructLineInfo(S, FuncLabel, S->getLine(), 0);
784       LineInfoGenerated = true;
785     }
786 
787     return;
788   }
789 
790   // Create a temporary label to remember the insn for lineinfo.
791   MCSymbol *LineSym = OS.getContext().createTempSymbol();
792   OS.EmitLabel(LineSym);
793 
794   // Construct the lineinfo.
795   auto SP = DL.get()->getScope()->getSubprogram();
796   constructLineInfo(SP, LineSym, DL.getLine(), DL.getCol());
797 
798   LineInfoGenerated = true;
799   PrevInstLoc = DL;
800 }
801 
802 void BTFDebug::processGlobals() {
803   // Collect all types referenced by globals.
804   const Module *M = MMI->getModule();
805   for (const GlobalVariable &Global : M->globals()) {
806     // Ignore external globals for now.
807     if (!Global.hasInitializer() && Global.hasExternalLinkage())
808       continue;
809 
810     SmallVector<DIGlobalVariableExpression *, 1> GVs;
811     Global.getDebugInfo(GVs);
812     uint32_t GVTypeId = 0;
813     for (auto *GVE : GVs) {
814       visitTypeEntry(GVE->getVariable()->getType().resolve(), GVTypeId);
815       break;
816     }
817 
818     // Only support the following globals:
819     //  . static variables
820     //  . non-static global variables with section attributes
821     // Essentially means:
822     //  . .bcc/.data/.rodata DataSec entities only contain static data
823     //  . Other DataSec entities contain static or initialized global data.
824     //    Initialized global data are mostly used for finding map key/value type
825     //    id's. Whether DataSec is readonly or not can be found from
826     //    corresponding ELF section flags.
827     auto Linkage = Global.getLinkage();
828     if (Linkage != GlobalValue::InternalLinkage &&
829         (Linkage != GlobalValue::ExternalLinkage || !Global.hasSection()))
830       continue;
831 
832     uint32_t GVarInfo = Linkage == GlobalValue::ExternalLinkage
833                             ? BTF::VAR_GLOBAL_ALLOCATED
834                             : BTF::VAR_STATIC;
835     auto VarEntry =
836         llvm::make_unique<BTFKindVar>(Global.getName(), GVTypeId, GVarInfo);
837     uint32_t VarId = addType(std::move(VarEntry));
838 
839     // Decide the section name.
840     std::string SecName;
841     if (Global.hasSection()) {
842       SecName = Global.getSection().str();
843     } else {
844       // data, bss, or readonly sections
845       if (Global.isConstant())
846         SecName += ".rodata";
847       else
848         SecName += Global.getInitializer()->isZeroValue() ? ".bss" : ".data";
849     }
850 
851     // Find or create a DataSec
852     if (DataSecEntries.find(SecName) == DataSecEntries.end()) {
853       DataSecEntries[SecName] = llvm::make_unique<BTFKindDataSec>(Asm, SecName);
854     }
855 
856     // Calculate symbol size
857     const DataLayout &DL = Global.getParent()->getDataLayout();
858     uint32_t Size = DL.getTypeAllocSize(Global.getType()->getElementType());
859 
860     DataSecEntries[SecName]->addVar(VarId, Asm->getSymbol(&Global), Size);
861   }
862 
863   for (auto &DataSec : DataSecEntries)
864     addType(std::move(DataSec.second));
865 }
866 
867 void BTFDebug::endModule() {
868   // Collect all global types/variables.
869   processGlobals();
870 
871   // Complete BTF type cross refereences.
872   for (const auto &TypeEntry : TypeEntries)
873     TypeEntry->completeType(*this);
874 
875   // Emit BTF sections.
876   emitBTFSection();
877   emitBTFExtSection();
878 }
879