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