BTFDebug.cpp 43 KB

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