blob: 3d08ca63a4b0500569d8567c5f16c79e47cbfd30 [file] [log] [blame]
Yonghong Song7b410ac2018-12-19 16:40:25 +00001//===- BTFDebug.cpp - BTF Generator ---------------------------------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Yonghong Song7b410ac2018-12-19 16:40:25 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file contains support for writing BTF debug info.
10//
11//===----------------------------------------------------------------------===//
12
13#include "BTFDebug.h"
Yonghong Songd3d88d02019-07-09 15:28:41 +000014#include "BPF.h"
15#include "BPFCORE.h"
16#include "MCTargetDesc/BPFMCTargetDesc.h"
Yonghong Song7b410ac2018-12-19 16:40:25 +000017#include "llvm/BinaryFormat/ELF.h"
18#include "llvm/CodeGen/AsmPrinter.h"
19#include "llvm/CodeGen/MachineModuleInfo.h"
20#include "llvm/MC/MCContext.h"
21#include "llvm/MC/MCObjectFileInfo.h"
22#include "llvm/MC/MCSectionELF.h"
23#include "llvm/MC/MCStreamer.h"
Fangrui Song83db8872019-04-02 16:15:46 +000024#include "llvm/Support/LineIterator.h"
Yonghong Song7b410ac2018-12-19 16:40:25 +000025
26using namespace llvm;
27
28static const char *BTFKindStr[] = {
29#define HANDLE_BTF_KIND(ID, NAME) "BTF_KIND_" #NAME,
30#include "BTF.def"
31};
32
33/// Emit a BTF common type.
34void BTFTypeBase::emitType(MCStreamer &OS) {
35 OS.AddComment(std::string(BTFKindStr[Kind]) + "(id = " + std::to_string(Id) +
36 ")");
37 OS.EmitIntValue(BTFType.NameOff, 4);
38 OS.AddComment("0x" + Twine::utohexstr(BTFType.Info));
39 OS.EmitIntValue(BTFType.Info, 4);
40 OS.EmitIntValue(BTFType.Size, 4);
41}
42
Yonghong Songd3d88d02019-07-09 15:28:41 +000043BTFTypeDerived::BTFTypeDerived(const DIDerivedType *DTy, unsigned Tag,
44 bool NeedsFixup)
45 : DTy(DTy), NeedsFixup(NeedsFixup) {
Yonghong Song7b410ac2018-12-19 16:40:25 +000046 switch (Tag) {
47 case dwarf::DW_TAG_pointer_type:
48 Kind = BTF::BTF_KIND_PTR;
49 break;
50 case dwarf::DW_TAG_const_type:
51 Kind = BTF::BTF_KIND_CONST;
52 break;
53 case dwarf::DW_TAG_volatile_type:
54 Kind = BTF::BTF_KIND_VOLATILE;
55 break;
56 case dwarf::DW_TAG_typedef:
57 Kind = BTF::BTF_KIND_TYPEDEF;
58 break;
59 case dwarf::DW_TAG_restrict_type:
60 Kind = BTF::BTF_KIND_RESTRICT;
61 break;
62 default:
63 llvm_unreachable("Unknown DIDerivedType Tag");
64 }
65 BTFType.Info = Kind << 24;
66}
67
68void BTFTypeDerived::completeType(BTFDebug &BDebug) {
Yonghong Songd3d88d02019-07-09 15:28:41 +000069 if (IsCompleted)
70 return;
71 IsCompleted = true;
72
Yonghong Song7b410ac2018-12-19 16:40:25 +000073 BTFType.NameOff = BDebug.addString(DTy->getName());
74
Yonghong Songd3d88d02019-07-09 15:28:41 +000075 if (NeedsFixup)
76 return;
77
Yonghong Song7b410ac2018-12-19 16:40:25 +000078 // The base type for PTR/CONST/VOLATILE could be void.
Fangrui Songda82ce92019-05-07 02:06:37 +000079 const DIType *ResolvedType = DTy->getBaseType();
Yonghong Song7b410ac2018-12-19 16:40:25 +000080 if (!ResolvedType) {
81 assert((Kind == BTF::BTF_KIND_PTR || Kind == BTF::BTF_KIND_CONST ||
82 Kind == BTF::BTF_KIND_VOLATILE) &&
83 "Invalid null basetype");
84 BTFType.Type = 0;
85 } else {
86 BTFType.Type = BDebug.getTypeId(ResolvedType);
87 }
88}
89
90void BTFTypeDerived::emitType(MCStreamer &OS) { BTFTypeBase::emitType(OS); }
91
Yonghong Songd3d88d02019-07-09 15:28:41 +000092void BTFTypeDerived::setPointeeType(uint32_t PointeeType) {
93 BTFType.Type = PointeeType;
94}
95
Yonghong Song7b410ac2018-12-19 16:40:25 +000096/// Represent a struct/union forward declaration.
97BTFTypeFwd::BTFTypeFwd(StringRef Name, bool IsUnion) : Name(Name) {
98 Kind = BTF::BTF_KIND_FWD;
99 BTFType.Info = IsUnion << 31 | Kind << 24;
100 BTFType.Type = 0;
101}
102
103void BTFTypeFwd::completeType(BTFDebug &BDebug) {
Yonghong Songd3d88d02019-07-09 15:28:41 +0000104 if (IsCompleted)
105 return;
106 IsCompleted = true;
107
Yonghong Song7b410ac2018-12-19 16:40:25 +0000108 BTFType.NameOff = BDebug.addString(Name);
109}
110
111void BTFTypeFwd::emitType(MCStreamer &OS) { BTFTypeBase::emitType(OS); }
112
113BTFTypeInt::BTFTypeInt(uint32_t Encoding, uint32_t SizeInBits,
114 uint32_t OffsetInBits, StringRef TypeName)
115 : Name(TypeName) {
116 // Translate IR int encoding to BTF int encoding.
117 uint8_t BTFEncoding;
118 switch (Encoding) {
119 case dwarf::DW_ATE_boolean:
120 BTFEncoding = BTF::INT_BOOL;
121 break;
122 case dwarf::DW_ATE_signed:
123 case dwarf::DW_ATE_signed_char:
124 BTFEncoding = BTF::INT_SIGNED;
125 break;
126 case dwarf::DW_ATE_unsigned:
127 case dwarf::DW_ATE_unsigned_char:
128 BTFEncoding = 0;
129 break;
130 default:
131 llvm_unreachable("Unknown BTFTypeInt Encoding");
132 }
133
134 Kind = BTF::BTF_KIND_INT;
135 BTFType.Info = Kind << 24;
136 BTFType.Size = roundupToBytes(SizeInBits);
137 IntVal = (BTFEncoding << 24) | OffsetInBits << 16 | SizeInBits;
138}
139
140void BTFTypeInt::completeType(BTFDebug &BDebug) {
Yonghong Songd3d88d02019-07-09 15:28:41 +0000141 if (IsCompleted)
142 return;
143 IsCompleted = true;
144
Yonghong Song7b410ac2018-12-19 16:40:25 +0000145 BTFType.NameOff = BDebug.addString(Name);
146}
147
148void BTFTypeInt::emitType(MCStreamer &OS) {
149 BTFTypeBase::emitType(OS);
150 OS.AddComment("0x" + Twine::utohexstr(IntVal));
151 OS.EmitIntValue(IntVal, 4);
152}
153
154BTFTypeEnum::BTFTypeEnum(const DICompositeType *ETy, uint32_t VLen) : ETy(ETy) {
155 Kind = BTF::BTF_KIND_ENUM;
156 BTFType.Info = Kind << 24 | VLen;
157 BTFType.Size = roundupToBytes(ETy->getSizeInBits());
158}
159
160void BTFTypeEnum::completeType(BTFDebug &BDebug) {
Yonghong Songd3d88d02019-07-09 15:28:41 +0000161 if (IsCompleted)
162 return;
163 IsCompleted = true;
164
Yonghong Song7b410ac2018-12-19 16:40:25 +0000165 BTFType.NameOff = BDebug.addString(ETy->getName());
166
167 DINodeArray Elements = ETy->getElements();
168 for (const auto Element : Elements) {
169 const auto *Enum = cast<DIEnumerator>(Element);
170
171 struct BTF::BTFEnum BTFEnum;
172 BTFEnum.NameOff = BDebug.addString(Enum->getName());
173 // BTF enum value is 32bit, enforce it.
174 BTFEnum.Val = static_cast<uint32_t>(Enum->getValue());
175 EnumValues.push_back(BTFEnum);
176 }
177}
178
179void BTFTypeEnum::emitType(MCStreamer &OS) {
180 BTFTypeBase::emitType(OS);
181 for (const auto &Enum : EnumValues) {
182 OS.EmitIntValue(Enum.NameOff, 4);
183 OS.EmitIntValue(Enum.Val, 4);
184 }
185}
186
Yonghong Song37d24a62019-08-02 23:16:44 +0000187BTFTypeArray::BTFTypeArray(uint32_t ElemTypeId, uint32_t NumElems) {
Yonghong Song7b410ac2018-12-19 16:40:25 +0000188 Kind = BTF::BTF_KIND_ARRAY;
Yonghong Song360a4e2c2019-03-28 21:59:49 +0000189 BTFType.NameOff = 0;
Yonghong Song7b410ac2018-12-19 16:40:25 +0000190 BTFType.Info = Kind << 24;
Yonghong Song7b410ac2018-12-19 16:40:25 +0000191 BTFType.Size = 0;
192
Yonghong Song360a4e2c2019-03-28 21:59:49 +0000193 ArrayInfo.ElemType = ElemTypeId;
194 ArrayInfo.Nelems = NumElems;
195}
196
197/// Represent a BTF array.
198void BTFTypeArray::completeType(BTFDebug &BDebug) {
Yonghong Songd3d88d02019-07-09 15:28:41 +0000199 if (IsCompleted)
200 return;
201 IsCompleted = true;
Yonghong Song7b410ac2018-12-19 16:40:25 +0000202
203 // The IR does not really have a type for the index.
204 // A special type for array index should have been
205 // created during initial type traversal. Just
206 // retrieve that type id.
207 ArrayInfo.IndexType = BDebug.getArrayIndexTypeId();
Yonghong Song7b410ac2018-12-19 16:40:25 +0000208}
209
210void BTFTypeArray::emitType(MCStreamer &OS) {
211 BTFTypeBase::emitType(OS);
212 OS.EmitIntValue(ArrayInfo.ElemType, 4);
213 OS.EmitIntValue(ArrayInfo.IndexType, 4);
214 OS.EmitIntValue(ArrayInfo.Nelems, 4);
215}
216
217/// Represent either a struct or a union.
218BTFTypeStruct::BTFTypeStruct(const DICompositeType *STy, bool IsStruct,
219 bool HasBitField, uint32_t Vlen)
220 : STy(STy), HasBitField(HasBitField) {
221 Kind = IsStruct ? BTF::BTF_KIND_STRUCT : BTF::BTF_KIND_UNION;
222 BTFType.Size = roundupToBytes(STy->getSizeInBits());
223 BTFType.Info = (HasBitField << 31) | (Kind << 24) | Vlen;
224}
225
226void BTFTypeStruct::completeType(BTFDebug &BDebug) {
Yonghong Songd3d88d02019-07-09 15:28:41 +0000227 if (IsCompleted)
228 return;
229 IsCompleted = true;
230
Yonghong Song7b410ac2018-12-19 16:40:25 +0000231 BTFType.NameOff = BDebug.addString(STy->getName());
232
233 // Add struct/union members.
234 const DINodeArray Elements = STy->getElements();
235 for (const auto *Element : Elements) {
236 struct BTF::BTFMember BTFMember;
237 const auto *DDTy = cast<DIDerivedType>(Element);
238
239 BTFMember.NameOff = BDebug.addString(DDTy->getName());
240 if (HasBitField) {
241 uint8_t BitFieldSize = DDTy->isBitField() ? DDTy->getSizeInBits() : 0;
242 BTFMember.Offset = BitFieldSize << 24 | DDTy->getOffsetInBits();
243 } else {
244 BTFMember.Offset = DDTy->getOffsetInBits();
245 }
Yonghong Song329abf22019-07-25 21:47:27 +0000246 const auto *BaseTy = DDTy->getBaseType();
247 BTFMember.Type = BDebug.getTypeId(BaseTy);
Yonghong Song7b410ac2018-12-19 16:40:25 +0000248 Members.push_back(BTFMember);
249 }
250}
251
252void BTFTypeStruct::emitType(MCStreamer &OS) {
253 BTFTypeBase::emitType(OS);
254 for (const auto &Member : Members) {
255 OS.EmitIntValue(Member.NameOff, 4);
256 OS.EmitIntValue(Member.Type, 4);
257 OS.AddComment("0x" + Twine::utohexstr(Member.Offset));
258 OS.EmitIntValue(Member.Offset, 4);
259 }
260}
261
Yonghong Songd3d88d02019-07-09 15:28:41 +0000262std::string BTFTypeStruct::getName() { return STy->getName(); }
263
Yonghong Song7b410ac2018-12-19 16:40:25 +0000264/// The Func kind represents both subprogram and pointee of function
265/// pointers. If the FuncName is empty, it represents a pointee of function
266/// pointer. Otherwise, it represents a subprogram. The func arg names
267/// are empty for pointee of function pointer case, and are valid names
268/// for subprogram.
269BTFTypeFuncProto::BTFTypeFuncProto(
270 const DISubroutineType *STy, uint32_t VLen,
271 const std::unordered_map<uint32_t, StringRef> &FuncArgNames)
272 : STy(STy), FuncArgNames(FuncArgNames) {
273 Kind = BTF::BTF_KIND_FUNC_PROTO;
274 BTFType.Info = (Kind << 24) | VLen;
275}
276
277void BTFTypeFuncProto::completeType(BTFDebug &BDebug) {
Yonghong Songd3d88d02019-07-09 15:28:41 +0000278 if (IsCompleted)
279 return;
280 IsCompleted = true;
281
Yonghong Song7b410ac2018-12-19 16:40:25 +0000282 DITypeRefArray Elements = STy->getTypeArray();
Fangrui Songda82ce92019-05-07 02:06:37 +0000283 auto RetType = Elements[0];
Yonghong Song7b410ac2018-12-19 16:40:25 +0000284 BTFType.Type = RetType ? BDebug.getTypeId(RetType) : 0;
285 BTFType.NameOff = 0;
286
287 // For null parameter which is typically the last one
288 // to represent the vararg, encode the NameOff/Type to be 0.
289 for (unsigned I = 1, N = Elements.size(); I < N; ++I) {
290 struct BTF::BTFParam Param;
Fangrui Songda82ce92019-05-07 02:06:37 +0000291 auto Element = Elements[I];
Yonghong Song7b410ac2018-12-19 16:40:25 +0000292 if (Element) {
293 Param.NameOff = BDebug.addString(FuncArgNames[I]);
294 Param.Type = BDebug.getTypeId(Element);
295 } else {
296 Param.NameOff = 0;
297 Param.Type = 0;
298 }
299 Parameters.push_back(Param);
300 }
301}
302
303void BTFTypeFuncProto::emitType(MCStreamer &OS) {
304 BTFTypeBase::emitType(OS);
305 for (const auto &Param : Parameters) {
306 OS.EmitIntValue(Param.NameOff, 4);
307 OS.EmitIntValue(Param.Type, 4);
308 }
309}
310
311BTFTypeFunc::BTFTypeFunc(StringRef FuncName, uint32_t ProtoTypeId)
312 : Name(FuncName) {
313 Kind = BTF::BTF_KIND_FUNC;
314 BTFType.Info = Kind << 24;
315 BTFType.Type = ProtoTypeId;
316}
317
318void BTFTypeFunc::completeType(BTFDebug &BDebug) {
Yonghong Songd3d88d02019-07-09 15:28:41 +0000319 if (IsCompleted)
320 return;
321 IsCompleted = true;
322
Yonghong Song7b410ac2018-12-19 16:40:25 +0000323 BTFType.NameOff = BDebug.addString(Name);
324}
325
326void BTFTypeFunc::emitType(MCStreamer &OS) { BTFTypeBase::emitType(OS); }
327
Yonghong Song6db6b562019-03-16 15:36:31 +0000328BTFKindVar::BTFKindVar(StringRef VarName, uint32_t TypeId, uint32_t VarInfo)
329 : Name(VarName) {
330 Kind = BTF::BTF_KIND_VAR;
331 BTFType.Info = Kind << 24;
332 BTFType.Type = TypeId;
333 Info = VarInfo;
334}
335
336void BTFKindVar::completeType(BTFDebug &BDebug) {
337 BTFType.NameOff = BDebug.addString(Name);
338}
339
340void BTFKindVar::emitType(MCStreamer &OS) {
341 BTFTypeBase::emitType(OS);
342 OS.EmitIntValue(Info, 4);
343}
344
345BTFKindDataSec::BTFKindDataSec(AsmPrinter *AsmPrt, std::string SecName)
346 : Asm(AsmPrt), Name(SecName) {
347 Kind = BTF::BTF_KIND_DATASEC;
348 BTFType.Info = Kind << 24;
349 BTFType.Size = 0;
350}
351
352void BTFKindDataSec::completeType(BTFDebug &BDebug) {
353 BTFType.NameOff = BDebug.addString(Name);
354 BTFType.Info |= Vars.size();
355}
356
357void BTFKindDataSec::emitType(MCStreamer &OS) {
358 BTFTypeBase::emitType(OS);
359
360 for (const auto &V : Vars) {
361 OS.EmitIntValue(std::get<0>(V), 4);
362 Asm->EmitLabelReference(std::get<1>(V), 4);
363 OS.EmitIntValue(std::get<2>(V), 4);
364 }
365}
366
Yonghong Song7b410ac2018-12-19 16:40:25 +0000367uint32_t BTFStringTable::addString(StringRef S) {
368 // Check whether the string already exists.
369 for (auto &OffsetM : OffsetToIdMap) {
370 if (Table[OffsetM.second] == S)
371 return OffsetM.first;
372 }
373 // Not find, add to the string table.
374 uint32_t Offset = Size;
375 OffsetToIdMap[Offset] = Table.size();
376 Table.push_back(S);
377 Size += S.size() + 1;
378 return Offset;
379}
380
381BTFDebug::BTFDebug(AsmPrinter *AP)
382 : DebugHandlerBase(AP), OS(*Asm->OutStreamer), SkipInstruction(false),
Yonghong Songd3d88d02019-07-09 15:28:41 +0000383 LineInfoGenerated(false), SecNameOff(0), ArrayIndexTypeId(0),
384 MapDefNotCollected(true) {
Yonghong Song7b410ac2018-12-19 16:40:25 +0000385 addString("\0");
386}
387
Yonghong Song6db6b562019-03-16 15:36:31 +0000388uint32_t BTFDebug::addType(std::unique_ptr<BTFTypeBase> TypeEntry,
389 const DIType *Ty) {
Yonghong Song7b410ac2018-12-19 16:40:25 +0000390 TypeEntry->setId(TypeEntries.size() + 1);
Yonghong Song6db6b562019-03-16 15:36:31 +0000391 uint32_t Id = TypeEntry->getId();
392 DIToIdMap[Ty] = Id;
Yonghong Song7b410ac2018-12-19 16:40:25 +0000393 TypeEntries.push_back(std::move(TypeEntry));
Yonghong Song6db6b562019-03-16 15:36:31 +0000394 return Id;
Yonghong Song7b410ac2018-12-19 16:40:25 +0000395}
396
397uint32_t BTFDebug::addType(std::unique_ptr<BTFTypeBase> TypeEntry) {
398 TypeEntry->setId(TypeEntries.size() + 1);
399 uint32_t Id = TypeEntry->getId();
400 TypeEntries.push_back(std::move(TypeEntry));
401 return Id;
402}
403
Yonghong Song6db6b562019-03-16 15:36:31 +0000404void BTFDebug::visitBasicType(const DIBasicType *BTy, uint32_t &TypeId) {
Yonghong Song7b410ac2018-12-19 16:40:25 +0000405 // Only int types are supported in BTF.
406 uint32_t Encoding = BTy->getEncoding();
407 if (Encoding != dwarf::DW_ATE_boolean && Encoding != dwarf::DW_ATE_signed &&
408 Encoding != dwarf::DW_ATE_signed_char &&
409 Encoding != dwarf::DW_ATE_unsigned &&
410 Encoding != dwarf::DW_ATE_unsigned_char)
411 return;
412
413 // Create a BTF type instance for this DIBasicType and put it into
414 // DIToIdMap for cross-type reference check.
Jonas Devlieghere0eaee542019-08-15 15:54:37 +0000415 auto TypeEntry = std::make_unique<BTFTypeInt>(
Yonghong Song7b410ac2018-12-19 16:40:25 +0000416 Encoding, BTy->getSizeInBits(), BTy->getOffsetInBits(), BTy->getName());
Yonghong Song6db6b562019-03-16 15:36:31 +0000417 TypeId = addType(std::move(TypeEntry), BTy);
Yonghong Song7b410ac2018-12-19 16:40:25 +0000418}
419
420/// Handle subprogram or subroutine types.
421void BTFDebug::visitSubroutineType(
422 const DISubroutineType *STy, bool ForSubprog,
423 const std::unordered_map<uint32_t, StringRef> &FuncArgNames,
424 uint32_t &TypeId) {
425 DITypeRefArray Elements = STy->getTypeArray();
426 uint32_t VLen = Elements.size() - 1;
427 if (VLen > BTF::MAX_VLEN)
428 return;
429
430 // Subprogram has a valid non-zero-length name, and the pointee of
431 // a function pointer has an empty name. The subprogram type will
432 // not be added to DIToIdMap as it should not be referenced by
433 // any other types.
Jonas Devlieghere0eaee542019-08-15 15:54:37 +0000434 auto TypeEntry = std::make_unique<BTFTypeFuncProto>(STy, VLen, FuncArgNames);
Yonghong Song7b410ac2018-12-19 16:40:25 +0000435 if (ForSubprog)
436 TypeId = addType(std::move(TypeEntry)); // For subprogram
437 else
Yonghong Song6db6b562019-03-16 15:36:31 +0000438 TypeId = addType(std::move(TypeEntry), STy); // For func ptr
Yonghong Song7b410ac2018-12-19 16:40:25 +0000439
440 // Visit return type and func arg types.
441 for (const auto Element : Elements) {
Fangrui Songda82ce92019-05-07 02:06:37 +0000442 visitTypeEntry(Element);
Yonghong Song7b410ac2018-12-19 16:40:25 +0000443 }
444}
445
446/// Handle structure/union types.
Yonghong Song6db6b562019-03-16 15:36:31 +0000447void BTFDebug::visitStructType(const DICompositeType *CTy, bool IsStruct,
448 uint32_t &TypeId) {
Yonghong Song7b410ac2018-12-19 16:40:25 +0000449 const DINodeArray Elements = CTy->getElements();
450 uint32_t VLen = Elements.size();
451 if (VLen > BTF::MAX_VLEN)
452 return;
453
454 // Check whether we have any bitfield members or not
455 bool HasBitField = false;
456 for (const auto *Element : Elements) {
457 auto E = cast<DIDerivedType>(Element);
458 if (E->isBitField()) {
459 HasBitField = true;
460 break;
461 }
462 }
463
464 auto TypeEntry =
Jonas Devlieghere0eaee542019-08-15 15:54:37 +0000465 std::make_unique<BTFTypeStruct>(CTy, IsStruct, HasBitField, VLen);
Yonghong Songd3d88d02019-07-09 15:28:41 +0000466 StructTypes.push_back(TypeEntry.get());
Yonghong Song6db6b562019-03-16 15:36:31 +0000467 TypeId = addType(std::move(TypeEntry), CTy);
Yonghong Song7b410ac2018-12-19 16:40:25 +0000468
469 // Visit all struct members.
470 for (const auto *Element : Elements)
471 visitTypeEntry(cast<DIDerivedType>(Element));
472}
473
Yonghong Song6db6b562019-03-16 15:36:31 +0000474void BTFDebug::visitArrayType(const DICompositeType *CTy, uint32_t &TypeId) {
Yonghong Song360a4e2c2019-03-28 21:59:49 +0000475 // Visit array element type.
Yonghong Songd3d88d02019-07-09 15:28:41 +0000476 uint32_t ElemTypeId, ElemSize;
477 const DIType *ElemType = CTy->getBaseType();
478 visitTypeEntry(ElemType, ElemTypeId, false, false);
Yonghong Song329abf22019-07-25 21:47:27 +0000479
480 // Strip qualifiers from element type to get accurate element size.
Yonghong Songd3d88d02019-07-09 15:28:41 +0000481 ElemSize = ElemType->getSizeInBits() >> 3;
Yonghong Song360a4e2c2019-03-28 21:59:49 +0000482
483 if (!CTy->getSizeInBits()) {
Jonas Devlieghere0eaee542019-08-15 15:54:37 +0000484 auto TypeEntry = std::make_unique<BTFTypeArray>(ElemTypeId, 0);
Yonghong Song360a4e2c2019-03-28 21:59:49 +0000485 ElemTypeId = addType(std::move(TypeEntry), CTy);
486 } else {
487 // Visit array dimensions.
488 DINodeArray Elements = CTy->getElements();
489 for (int I = Elements.size() - 1; I >= 0; --I) {
490 if (auto *Element = dyn_cast_or_null<DINode>(Elements[I]))
491 if (Element->getTag() == dwarf::DW_TAG_subrange_type) {
492 const DISubrange *SR = cast<DISubrange>(Element);
493 auto *CI = SR->getCount().dyn_cast<ConstantInt *>();
494 int64_t Count = CI->getSExtValue();
495
Yonghong Songd3d88d02019-07-09 15:28:41 +0000496 auto TypeEntry =
Jonas Devlieghere0eaee542019-08-15 15:54:37 +0000497 std::make_unique<BTFTypeArray>(ElemTypeId, Count);
Yonghong Song360a4e2c2019-03-28 21:59:49 +0000498 if (I == 0)
499 ElemTypeId = addType(std::move(TypeEntry), CTy);
500 else
501 ElemTypeId = addType(std::move(TypeEntry));
Yonghong Songd3d88d02019-07-09 15:28:41 +0000502 ElemSize = ElemSize * Count;
Yonghong Song360a4e2c2019-03-28 21:59:49 +0000503 }
504 }
505 }
506
507 // The array TypeId is the type id of the outermost dimension.
508 TypeId = ElemTypeId;
Yonghong Song7b410ac2018-12-19 16:40:25 +0000509
510 // The IR does not have a type for array index while BTF wants one.
511 // So create an array index type if there is none.
512 if (!ArrayIndexTypeId) {
Jonas Devlieghere0eaee542019-08-15 15:54:37 +0000513 auto TypeEntry = std::make_unique<BTFTypeInt>(dwarf::DW_ATE_unsigned, 32,
Yonghong Song7b410ac2018-12-19 16:40:25 +0000514 0, "__ARRAY_SIZE_TYPE__");
515 ArrayIndexTypeId = addType(std::move(TypeEntry));
516 }
Yonghong Song7b410ac2018-12-19 16:40:25 +0000517}
518
Yonghong Song6db6b562019-03-16 15:36:31 +0000519void BTFDebug::visitEnumType(const DICompositeType *CTy, uint32_t &TypeId) {
Yonghong Song7b410ac2018-12-19 16:40:25 +0000520 DINodeArray Elements = CTy->getElements();
521 uint32_t VLen = Elements.size();
522 if (VLen > BTF::MAX_VLEN)
523 return;
524
Jonas Devlieghere0eaee542019-08-15 15:54:37 +0000525 auto TypeEntry = std::make_unique<BTFTypeEnum>(CTy, VLen);
Yonghong Song6db6b562019-03-16 15:36:31 +0000526 TypeId = addType(std::move(TypeEntry), CTy);
Yonghong Song7b410ac2018-12-19 16:40:25 +0000527 // No need to visit base type as BTF does not encode it.
528}
529
530/// Handle structure/union forward declarations.
Yonghong Song6db6b562019-03-16 15:36:31 +0000531void BTFDebug::visitFwdDeclType(const DICompositeType *CTy, bool IsUnion,
532 uint32_t &TypeId) {
Jonas Devlieghere0eaee542019-08-15 15:54:37 +0000533 auto TypeEntry = std::make_unique<BTFTypeFwd>(CTy->getName(), IsUnion);
Yonghong Song6db6b562019-03-16 15:36:31 +0000534 TypeId = addType(std::move(TypeEntry), CTy);
Yonghong Song7b410ac2018-12-19 16:40:25 +0000535}
536
537/// Handle structure, union, array and enumeration types.
Yonghong Song6db6b562019-03-16 15:36:31 +0000538void BTFDebug::visitCompositeType(const DICompositeType *CTy,
539 uint32_t &TypeId) {
Yonghong Song7b410ac2018-12-19 16:40:25 +0000540 auto Tag = CTy->getTag();
541 if (Tag == dwarf::DW_TAG_structure_type || Tag == dwarf::DW_TAG_union_type) {
542 // Handle forward declaration differently as it does not have members.
543 if (CTy->isForwardDecl())
Yonghong Song6db6b562019-03-16 15:36:31 +0000544 visitFwdDeclType(CTy, Tag == dwarf::DW_TAG_union_type, TypeId);
Yonghong Song7b410ac2018-12-19 16:40:25 +0000545 else
Yonghong Song6db6b562019-03-16 15:36:31 +0000546 visitStructType(CTy, Tag == dwarf::DW_TAG_structure_type, TypeId);
Yonghong Song7b410ac2018-12-19 16:40:25 +0000547 } else if (Tag == dwarf::DW_TAG_array_type)
Yonghong Song6db6b562019-03-16 15:36:31 +0000548 visitArrayType(CTy, TypeId);
Yonghong Song7b410ac2018-12-19 16:40:25 +0000549 else if (Tag == dwarf::DW_TAG_enumeration_type)
Yonghong Song6db6b562019-03-16 15:36:31 +0000550 visitEnumType(CTy, TypeId);
Yonghong Song7b410ac2018-12-19 16:40:25 +0000551}
552
553/// Handle pointer, typedef, const, volatile, restrict and member types.
Yonghong Songd3d88d02019-07-09 15:28:41 +0000554void BTFDebug::visitDerivedType(const DIDerivedType *DTy, uint32_t &TypeId,
555 bool CheckPointer, bool SeenPointer) {
Yonghong Song7b410ac2018-12-19 16:40:25 +0000556 unsigned Tag = DTy->getTag();
557
Yonghong Songd3d88d02019-07-09 15:28:41 +0000558 /// Try to avoid chasing pointees, esp. structure pointees which may
559 /// unnecessary bring in a lot of types.
560 if (CheckPointer && !SeenPointer) {
561 SeenPointer = Tag == dwarf::DW_TAG_pointer_type;
562 }
563
564 if (CheckPointer && SeenPointer) {
565 const DIType *Base = DTy->getBaseType();
566 if (Base) {
567 if (const auto *CTy = dyn_cast<DICompositeType>(Base)) {
568 auto CTag = CTy->getTag();
569 if ((CTag == dwarf::DW_TAG_structure_type ||
570 CTag == dwarf::DW_TAG_union_type) &&
571 !CTy->isForwardDecl()) {
572 /// Find a candidate, generate a fixup. Later on the struct/union
573 /// pointee type will be replaced with either a real type or
574 /// a forward declaration.
Jonas Devlieghere0eaee542019-08-15 15:54:37 +0000575 auto TypeEntry = std::make_unique<BTFTypeDerived>(DTy, Tag, true);
Yonghong Songd3d88d02019-07-09 15:28:41 +0000576 auto &Fixup = FixupDerivedTypes[CTy->getName()];
577 Fixup.first = CTag == dwarf::DW_TAG_union_type;
578 Fixup.second.push_back(TypeEntry.get());
579 TypeId = addType(std::move(TypeEntry), DTy);
580 return;
581 }
582 }
583 }
584 }
585
Yonghong Song7b410ac2018-12-19 16:40:25 +0000586 if (Tag == dwarf::DW_TAG_pointer_type || Tag == dwarf::DW_TAG_typedef ||
587 Tag == dwarf::DW_TAG_const_type || Tag == dwarf::DW_TAG_volatile_type ||
588 Tag == dwarf::DW_TAG_restrict_type) {
Jonas Devlieghere0eaee542019-08-15 15:54:37 +0000589 auto TypeEntry = std::make_unique<BTFTypeDerived>(DTy, Tag, false);
Yonghong Song6db6b562019-03-16 15:36:31 +0000590 TypeId = addType(std::move(TypeEntry), DTy);
Yonghong Song7b410ac2018-12-19 16:40:25 +0000591 } else if (Tag != dwarf::DW_TAG_member) {
592 return;
593 }
594
595 // Visit base type of pointer, typedef, const, volatile, restrict or
596 // struct/union member.
Yonghong Songded9a442019-03-22 01:30:50 +0000597 uint32_t TempTypeId = 0;
Yonghong Songd3d88d02019-07-09 15:28:41 +0000598 if (Tag == dwarf::DW_TAG_member)
599 visitTypeEntry(DTy->getBaseType(), TempTypeId, true, false);
600 else
601 visitTypeEntry(DTy->getBaseType(), TempTypeId, CheckPointer, SeenPointer);
Yonghong Song7b410ac2018-12-19 16:40:25 +0000602}
603
Yonghong Songd3d88d02019-07-09 15:28:41 +0000604void BTFDebug::visitTypeEntry(const DIType *Ty, uint32_t &TypeId,
605 bool CheckPointer, bool SeenPointer) {
Yonghong Song6db6b562019-03-16 15:36:31 +0000606 if (!Ty || DIToIdMap.find(Ty) != DIToIdMap.end()) {
607 TypeId = DIToIdMap[Ty];
Yonghong Song7b410ac2018-12-19 16:40:25 +0000608 return;
Yonghong Song6db6b562019-03-16 15:36:31 +0000609 }
Yonghong Song7b410ac2018-12-19 16:40:25 +0000610
Yonghong Song7b410ac2018-12-19 16:40:25 +0000611 if (const auto *BTy = dyn_cast<DIBasicType>(Ty))
Yonghong Song6db6b562019-03-16 15:36:31 +0000612 visitBasicType(BTy, TypeId);
Yonghong Song7b410ac2018-12-19 16:40:25 +0000613 else if (const auto *STy = dyn_cast<DISubroutineType>(Ty))
614 visitSubroutineType(STy, false, std::unordered_map<uint32_t, StringRef>(),
615 TypeId);
616 else if (const auto *CTy = dyn_cast<DICompositeType>(Ty))
Yonghong Song6db6b562019-03-16 15:36:31 +0000617 visitCompositeType(CTy, TypeId);
Yonghong Song7b410ac2018-12-19 16:40:25 +0000618 else if (const auto *DTy = dyn_cast<DIDerivedType>(Ty))
Yonghong Songd3d88d02019-07-09 15:28:41 +0000619 visitDerivedType(DTy, TypeId, CheckPointer, SeenPointer);
Yonghong Song7b410ac2018-12-19 16:40:25 +0000620 else
621 llvm_unreachable("Unknown DIType");
622}
623
Yonghong Song6db6b562019-03-16 15:36:31 +0000624void BTFDebug::visitTypeEntry(const DIType *Ty) {
625 uint32_t TypeId;
Yonghong Songd3d88d02019-07-09 15:28:41 +0000626 visitTypeEntry(Ty, TypeId, false, false);
627}
628
629void BTFDebug::visitMapDefType(const DIType *Ty, uint32_t &TypeId) {
630 if (!Ty || DIToIdMap.find(Ty) != DIToIdMap.end()) {
631 TypeId = DIToIdMap[Ty];
632 return;
633 }
634
635 // MapDef type is a struct type
636 const auto *CTy = dyn_cast<DICompositeType>(Ty);
637 if (!CTy)
638 return;
639
640 auto Tag = CTy->getTag();
641 if (Tag != dwarf::DW_TAG_structure_type || CTy->isForwardDecl())
642 return;
643
644 // Record this type
645 const DINodeArray Elements = CTy->getElements();
646 bool HasBitField = false;
647 for (const auto *Element : Elements) {
648 auto E = cast<DIDerivedType>(Element);
649 if (E->isBitField()) {
650 HasBitField = true;
651 break;
652 }
653 }
654
655 auto TypeEntry =
Jonas Devlieghere0eaee542019-08-15 15:54:37 +0000656 std::make_unique<BTFTypeStruct>(CTy, true, HasBitField, Elements.size());
Yonghong Songd3d88d02019-07-09 15:28:41 +0000657 StructTypes.push_back(TypeEntry.get());
658 TypeId = addType(std::move(TypeEntry), CTy);
659
660 // Visit all struct members
661 for (const auto *Element : Elements) {
662 const auto *MemberType = cast<DIDerivedType>(Element);
663 visitTypeEntry(MemberType->getBaseType());
664 }
Yonghong Song6db6b562019-03-16 15:36:31 +0000665}
666
Yonghong Song7b410ac2018-12-19 16:40:25 +0000667/// Read file contents from the actual file or from the source
668std::string BTFDebug::populateFileContent(const DISubprogram *SP) {
669 auto File = SP->getFile();
670 std::string FileName;
671
Yonghong Songfa365402019-02-02 05:54:59 +0000672 if (!File->getFilename().startswith("/") && File->getDirectory().size())
Yonghong Song7b410ac2018-12-19 16:40:25 +0000673 FileName = File->getDirectory().str() + "/" + File->getFilename().str();
674 else
675 FileName = File->getFilename();
676
677 // No need to populate the contends if it has been populated!
678 if (FileContent.find(FileName) != FileContent.end())
679 return FileName;
680
681 std::vector<std::string> Content;
682 std::string Line;
683 Content.push_back(Line); // Line 0 for empty string
684
Fangrui Song83db8872019-04-02 16:15:46 +0000685 std::unique_ptr<MemoryBuffer> Buf;
Yonghong Song7b410ac2018-12-19 16:40:25 +0000686 auto Source = File->getSource();
Fangrui Song83db8872019-04-02 16:15:46 +0000687 if (Source)
688 Buf = MemoryBuffer::getMemBufferCopy(*Source);
689 else if (ErrorOr<std::unique_ptr<MemoryBuffer>> BufOrErr =
690 MemoryBuffer::getFile(FileName))
691 Buf = std::move(*BufOrErr);
692 if (Buf)
693 for (line_iterator I(*Buf, false), E; I != E; ++I)
694 Content.push_back(*I);
Yonghong Song7b410ac2018-12-19 16:40:25 +0000695
696 FileContent[FileName] = Content;
697 return FileName;
698}
699
700void BTFDebug::constructLineInfo(const DISubprogram *SP, MCSymbol *Label,
701 uint32_t Line, uint32_t Column) {
702 std::string FileName = populateFileContent(SP);
703 BTFLineInfo LineInfo;
704
705 LineInfo.Label = Label;
706 LineInfo.FileNameOff = addString(FileName);
707 // If file content is not available, let LineOff = 0.
708 if (Line < FileContent[FileName].size())
709 LineInfo.LineOff = addString(FileContent[FileName][Line]);
710 else
711 LineInfo.LineOff = 0;
712 LineInfo.LineNum = Line;
713 LineInfo.ColumnNum = Column;
714 LineInfoTable[SecNameOff].push_back(LineInfo);
715}
716
717void BTFDebug::emitCommonHeader() {
718 OS.AddComment("0x" + Twine::utohexstr(BTF::MAGIC));
719 OS.EmitIntValue(BTF::MAGIC, 2);
720 OS.EmitIntValue(BTF::VERSION, 1);
721 OS.EmitIntValue(0, 1);
722}
723
724void BTFDebug::emitBTFSection() {
Yonghong Songd82247c2019-03-05 01:01:21 +0000725 // Do not emit section if no types and only "" string.
726 if (!TypeEntries.size() && StringTable.getSize() == 1)
727 return;
728
Yonghong Song7b410ac2018-12-19 16:40:25 +0000729 MCContext &Ctx = OS.getContext();
730 OS.SwitchSection(Ctx.getELFSection(".BTF", ELF::SHT_PROGBITS, 0));
731
732 // Emit header.
733 emitCommonHeader();
734 OS.EmitIntValue(BTF::HeaderSize, 4);
735
736 uint32_t TypeLen = 0, StrLen;
737 for (const auto &TypeEntry : TypeEntries)
738 TypeLen += TypeEntry->getSize();
739 StrLen = StringTable.getSize();
740
741 OS.EmitIntValue(0, 4);
742 OS.EmitIntValue(TypeLen, 4);
743 OS.EmitIntValue(TypeLen, 4);
744 OS.EmitIntValue(StrLen, 4);
745
746 // Emit type table.
747 for (const auto &TypeEntry : TypeEntries)
748 TypeEntry->emitType(OS);
749
750 // Emit string table.
751 uint32_t StringOffset = 0;
752 for (const auto &S : StringTable.getTable()) {
753 OS.AddComment("string offset=" + std::to_string(StringOffset));
754 OS.EmitBytes(S);
755 OS.EmitBytes(StringRef("\0", 1));
756 StringOffset += S.size() + 1;
757 }
758}
759
760void BTFDebug::emitBTFExtSection() {
Yonghong Songd82247c2019-03-05 01:01:21 +0000761 // Do not emit section if empty FuncInfoTable and LineInfoTable.
Yonghong Songd3d88d02019-07-09 15:28:41 +0000762 if (!FuncInfoTable.size() && !LineInfoTable.size() &&
763 !OffsetRelocTable.size() && !ExternRelocTable.size())
Yonghong Songd82247c2019-03-05 01:01:21 +0000764 return;
765
Yonghong Song7b410ac2018-12-19 16:40:25 +0000766 MCContext &Ctx = OS.getContext();
767 OS.SwitchSection(Ctx.getELFSection(".BTF.ext", ELF::SHT_PROGBITS, 0));
768
769 // Emit header.
770 emitCommonHeader();
771 OS.EmitIntValue(BTF::ExtHeaderSize, 4);
772
773 // Account for FuncInfo/LineInfo record size as well.
774 uint32_t FuncLen = 4, LineLen = 4;
Yonghong Songd3d88d02019-07-09 15:28:41 +0000775 // Do not account for optional OffsetReloc/ExternReloc.
776 uint32_t OffsetRelocLen = 0, ExternRelocLen = 0;
Yonghong Song7b410ac2018-12-19 16:40:25 +0000777 for (const auto &FuncSec : FuncInfoTable) {
778 FuncLen += BTF::SecFuncInfoSize;
779 FuncLen += FuncSec.second.size() * BTF::BPFFuncInfoSize;
780 }
781 for (const auto &LineSec : LineInfoTable) {
782 LineLen += BTF::SecLineInfoSize;
783 LineLen += LineSec.second.size() * BTF::BPFLineInfoSize;
784 }
Yonghong Songd3d88d02019-07-09 15:28:41 +0000785 for (const auto &OffsetRelocSec : OffsetRelocTable) {
786 OffsetRelocLen += BTF::SecOffsetRelocSize;
787 OffsetRelocLen += OffsetRelocSec.second.size() * BTF::BPFOffsetRelocSize;
788 }
789 for (const auto &ExternRelocSec : ExternRelocTable) {
790 ExternRelocLen += BTF::SecExternRelocSize;
791 ExternRelocLen += ExternRelocSec.second.size() * BTF::BPFExternRelocSize;
792 }
793
794 if (OffsetRelocLen)
795 OffsetRelocLen += 4;
796 if (ExternRelocLen)
797 ExternRelocLen += 4;
Yonghong Song7b410ac2018-12-19 16:40:25 +0000798
799 OS.EmitIntValue(0, 4);
800 OS.EmitIntValue(FuncLen, 4);
801 OS.EmitIntValue(FuncLen, 4);
802 OS.EmitIntValue(LineLen, 4);
Yonghong Songd3d88d02019-07-09 15:28:41 +0000803 OS.EmitIntValue(FuncLen + LineLen, 4);
804 OS.EmitIntValue(OffsetRelocLen, 4);
805 OS.EmitIntValue(FuncLen + LineLen + OffsetRelocLen, 4);
806 OS.EmitIntValue(ExternRelocLen, 4);
Yonghong Song7b410ac2018-12-19 16:40:25 +0000807
808 // Emit func_info table.
809 OS.AddComment("FuncInfo");
810 OS.EmitIntValue(BTF::BPFFuncInfoSize, 4);
811 for (const auto &FuncSec : FuncInfoTable) {
812 OS.AddComment("FuncInfo section string offset=" +
813 std::to_string(FuncSec.first));
814 OS.EmitIntValue(FuncSec.first, 4);
815 OS.EmitIntValue(FuncSec.second.size(), 4);
816 for (const auto &FuncInfo : FuncSec.second) {
817 Asm->EmitLabelReference(FuncInfo.Label, 4);
818 OS.EmitIntValue(FuncInfo.TypeId, 4);
819 }
820 }
821
822 // Emit line_info table.
823 OS.AddComment("LineInfo");
824 OS.EmitIntValue(BTF::BPFLineInfoSize, 4);
825 for (const auto &LineSec : LineInfoTable) {
826 OS.AddComment("LineInfo section string offset=" +
827 std::to_string(LineSec.first));
828 OS.EmitIntValue(LineSec.first, 4);
829 OS.EmitIntValue(LineSec.second.size(), 4);
830 for (const auto &LineInfo : LineSec.second) {
831 Asm->EmitLabelReference(LineInfo.Label, 4);
832 OS.EmitIntValue(LineInfo.FileNameOff, 4);
833 OS.EmitIntValue(LineInfo.LineOff, 4);
834 OS.AddComment("Line " + std::to_string(LineInfo.LineNum) + " Col " +
835 std::to_string(LineInfo.ColumnNum));
836 OS.EmitIntValue(LineInfo.LineNum << 10 | LineInfo.ColumnNum, 4);
837 }
838 }
Yonghong Songd3d88d02019-07-09 15:28:41 +0000839
840 // Emit offset reloc table.
841 if (OffsetRelocLen) {
842 OS.AddComment("OffsetReloc");
843 OS.EmitIntValue(BTF::BPFOffsetRelocSize, 4);
844 for (const auto &OffsetRelocSec : OffsetRelocTable) {
845 OS.AddComment("Offset reloc section string offset=" +
846 std::to_string(OffsetRelocSec.first));
847 OS.EmitIntValue(OffsetRelocSec.first, 4);
848 OS.EmitIntValue(OffsetRelocSec.second.size(), 4);
849 for (const auto &OffsetRelocInfo : OffsetRelocSec.second) {
850 Asm->EmitLabelReference(OffsetRelocInfo.Label, 4);
851 OS.EmitIntValue(OffsetRelocInfo.TypeID, 4);
852 OS.EmitIntValue(OffsetRelocInfo.OffsetNameOff, 4);
853 }
854 }
855 }
856
857 // Emit extern reloc table.
858 if (ExternRelocLen) {
859 OS.AddComment("ExternReloc");
860 OS.EmitIntValue(BTF::BPFExternRelocSize, 4);
861 for (const auto &ExternRelocSec : ExternRelocTable) {
862 OS.AddComment("Extern reloc section string offset=" +
863 std::to_string(ExternRelocSec.first));
864 OS.EmitIntValue(ExternRelocSec.first, 4);
865 OS.EmitIntValue(ExternRelocSec.second.size(), 4);
866 for (const auto &ExternRelocInfo : ExternRelocSec.second) {
867 Asm->EmitLabelReference(ExternRelocInfo.Label, 4);
868 OS.EmitIntValue(ExternRelocInfo.ExternNameOff, 4);
869 }
870 }
871 }
Yonghong Song7b410ac2018-12-19 16:40:25 +0000872}
873
874void BTFDebug::beginFunctionImpl(const MachineFunction *MF) {
875 auto *SP = MF->getFunction().getSubprogram();
876 auto *Unit = SP->getUnit();
877
878 if (Unit->getEmissionKind() == DICompileUnit::NoDebug) {
879 SkipInstruction = true;
880 return;
881 }
882 SkipInstruction = false;
883
Yonghong Songd3d88d02019-07-09 15:28:41 +0000884 // Collect MapDef types. Map definition needs to collect
885 // pointee types. Do it first. Otherwise, for the following
886 // case:
887 // struct m { ...};
888 // struct t {
889 // struct m *key;
890 // };
891 // foo(struct t *arg);
892 //
893 // struct mapdef {
894 // ...
895 // struct m *key;
896 // ...
897 // } __attribute__((section(".maps"))) hash_map;
898 //
899 // If subroutine foo is traversed first, a type chain
900 // "ptr->struct m(fwd)" will be created and later on
901 // when traversing mapdef, since "ptr->struct m" exists,
902 // the traversal of "struct m" will be omitted.
903 if (MapDefNotCollected) {
904 processGlobals(true);
905 MapDefNotCollected = false;
906 }
907
Yonghong Song7b410ac2018-12-19 16:40:25 +0000908 // Collect all types locally referenced in this function.
909 // Use RetainedNodes so we can collect all argument names
910 // even if the argument is not used.
911 std::unordered_map<uint32_t, StringRef> FuncArgNames;
912 for (const DINode *DN : SP->getRetainedNodes()) {
913 if (const auto *DV = dyn_cast<DILocalVariable>(DN)) {
Yonghong Song7b410ac2018-12-19 16:40:25 +0000914 // Collect function arguments for subprogram func type.
915 uint32_t Arg = DV->getArg();
Yonghong Songcacac052019-03-15 05:51:25 +0000916 if (Arg) {
Fangrui Songda82ce92019-05-07 02:06:37 +0000917 visitTypeEntry(DV->getType());
Yonghong Song7b410ac2018-12-19 16:40:25 +0000918 FuncArgNames[Arg] = DV->getName();
Yonghong Songcacac052019-03-15 05:51:25 +0000919 }
Yonghong Song7b410ac2018-12-19 16:40:25 +0000920 }
921 }
922
923 // Construct subprogram func proto type.
924 uint32_t ProtoTypeId;
925 visitSubroutineType(SP->getType(), true, FuncArgNames, ProtoTypeId);
926
927 // Construct subprogram func type
928 auto FuncTypeEntry =
Jonas Devlieghere0eaee542019-08-15 15:54:37 +0000929 std::make_unique<BTFTypeFunc>(SP->getName(), ProtoTypeId);
Yonghong Song7b410ac2018-12-19 16:40:25 +0000930 uint32_t FuncTypeId = addType(std::move(FuncTypeEntry));
931
Yonghong Songd3d88d02019-07-09 15:28:41 +0000932 for (const auto &TypeEntry : TypeEntries)
933 TypeEntry->completeType(*this);
934
Yonghong Song7b410ac2018-12-19 16:40:25 +0000935 // Construct funcinfo and the first lineinfo for the function.
936 MCSymbol *FuncLabel = Asm->getFunctionBegin();
937 BTFFuncInfo FuncInfo;
938 FuncInfo.Label = FuncLabel;
939 FuncInfo.TypeId = FuncTypeId;
940 if (FuncLabel->isInSection()) {
941 MCSection &Section = FuncLabel->getSection();
942 const MCSectionELF *SectionELF = dyn_cast<MCSectionELF>(&Section);
943 assert(SectionELF && "Null section for Function Label");
944 SecNameOff = addString(SectionELF->getSectionName());
945 } else {
946 SecNameOff = addString(".text");
947 }
948 FuncInfoTable[SecNameOff].push_back(FuncInfo);
949}
950
951void BTFDebug::endFunctionImpl(const MachineFunction *MF) {
952 SkipInstruction = false;
953 LineInfoGenerated = false;
954 SecNameOff = 0;
955}
956
Yonghong Songd3d88d02019-07-09 15:28:41 +0000957/// On-demand populate struct types as requested from abstract member
958/// accessing.
959unsigned BTFDebug::populateStructType(const DIType *Ty) {
960 unsigned Id;
961 visitTypeEntry(Ty, Id, false, false);
962 for (const auto &TypeEntry : TypeEntries)
963 TypeEntry->completeType(*this);
964 return Id;
965}
966
Yonghong Songd3d88d02019-07-09 15:28:41 +0000967/// Generate a struct member offset relocation.
968void BTFDebug::generateOffsetReloc(const MachineInstr *MI,
969 const MCSymbol *ORSym, DIType *RootTy,
970 StringRef AccessPattern) {
Yonghong Songd3d88d02019-07-09 15:28:41 +0000971 unsigned RootId = populateStructType(RootTy);
Yonghong Song37d24a62019-08-02 23:16:44 +0000972 size_t FirstDollar = AccessPattern.find_first_of('$');
973 size_t FirstColon = AccessPattern.find_first_of(':');
974 StringRef IndexPattern = AccessPattern.substr(FirstDollar + 1);
975 StringRef OffsetStr = AccessPattern.substr(FirstColon + 1,
976 FirstDollar - FirstColon);
Yonghong Songd3d88d02019-07-09 15:28:41 +0000977
978 BTFOffsetReloc OffsetReloc;
979 OffsetReloc.Label = ORSym;
Yonghong Song37d24a62019-08-02 23:16:44 +0000980 OffsetReloc.OffsetNameOff = addString(IndexPattern);
Yonghong Songd3d88d02019-07-09 15:28:41 +0000981 OffsetReloc.TypeID = RootId;
Yonghong Song37d24a62019-08-02 23:16:44 +0000982 AccessOffsets[AccessPattern.str()] = std::stoi(OffsetStr);
Yonghong Songd3d88d02019-07-09 15:28:41 +0000983 OffsetRelocTable[SecNameOff].push_back(OffsetReloc);
984}
985
986void BTFDebug::processLDimm64(const MachineInstr *MI) {
987 // If the insn is an LD_imm64, the following two cases
988 // will generate an .BTF.ext record.
989 //
990 // If the insn is "r2 = LD_imm64 @__BTF_...",
991 // add this insn into the .BTF.ext OffsetReloc subsection.
992 // Relocation looks like:
993 // . SecName:
994 // . InstOffset
995 // . TypeID
996 // . OffSetNameOff
997 // Later, the insn is replaced with "r2 = <offset>"
998 // where "<offset>" equals to the offset based on current
999 // type definitions.
1000 //
1001 // If the insn is "r2 = LD_imm64 @VAR" and VAR is
1002 // a patchable external global, add this insn into the .BTF.ext
1003 // ExternReloc subsection.
1004 // Relocation looks like:
1005 // . SecName:
1006 // . InstOffset
1007 // . ExternNameOff
1008 // Later, the insn is replaced with "r2 = <value>" or
1009 // "LD_imm64 r2, <value>" where "<value>" = 0.
1010
1011 // check whether this is a candidate or not
1012 const MachineOperand &MO = MI->getOperand(1);
1013 if (MO.isGlobal()) {
1014 const GlobalValue *GVal = MO.getGlobal();
1015 auto *GVar = dyn_cast<GlobalVariable>(GVal);
1016 if (GVar && GVar->hasAttribute(BPFCoreSharedInfo::AmaAttr)) {
1017 MCSymbol *ORSym = OS.getContext().createTempSymbol();
1018 OS.EmitLabel(ORSym);
1019
1020 MDNode *MDN = GVar->getMetadata(LLVMContext::MD_preserve_access_index);
1021 DIType *Ty = dyn_cast<DIType>(MDN);
1022 generateOffsetReloc(MI, ORSym, Ty, GVar->getName());
1023 } else if (GVar && !GVar->hasInitializer() && GVar->hasExternalLinkage() &&
1024 GVar->getSection() == BPFCoreSharedInfo::PatchableExtSecName) {
1025 MCSymbol *ORSym = OS.getContext().createTempSymbol();
1026 OS.EmitLabel(ORSym);
1027
1028 BTFExternReloc ExternReloc;
1029 ExternReloc.Label = ORSym;
1030 ExternReloc.ExternNameOff = addString(GVar->getName());
1031 ExternRelocTable[SecNameOff].push_back(ExternReloc);
1032 }
1033 }
1034}
1035
Yonghong Song7b410ac2018-12-19 16:40:25 +00001036void BTFDebug::beginInstruction(const MachineInstr *MI) {
1037 DebugHandlerBase::beginInstruction(MI);
1038
1039 if (SkipInstruction || MI->isMetaInstruction() ||
1040 MI->getFlag(MachineInstr::FrameSetup))
1041 return;
1042
1043 if (MI->isInlineAsm()) {
1044 // Count the number of register definitions to find the asm string.
1045 unsigned NumDefs = 0;
1046 for (; MI->getOperand(NumDefs).isReg() && MI->getOperand(NumDefs).isDef();
1047 ++NumDefs)
1048 ;
1049
1050 // Skip this inline asm instruction if the asmstr is empty.
1051 const char *AsmStr = MI->getOperand(NumDefs).getSymbolName();
1052 if (AsmStr[0] == 0)
1053 return;
1054 }
1055
Yonghong Songd3d88d02019-07-09 15:28:41 +00001056 if (MI->getOpcode() == BPF::LD_imm64)
1057 processLDimm64(MI);
1058
Yonghong Song7b410ac2018-12-19 16:40:25 +00001059 // Skip this instruction if no DebugLoc or the DebugLoc
1060 // is the same as the previous instruction.
1061 const DebugLoc &DL = MI->getDebugLoc();
1062 if (!DL || PrevInstLoc == DL) {
1063 // This instruction will be skipped, no LineInfo has
1064 // been generated, construct one based on function signature.
1065 if (LineInfoGenerated == false) {
1066 auto *S = MI->getMF()->getFunction().getSubprogram();
1067 MCSymbol *FuncLabel = Asm->getFunctionBegin();
1068 constructLineInfo(S, FuncLabel, S->getLine(), 0);
1069 LineInfoGenerated = true;
1070 }
1071
1072 return;
1073 }
1074
1075 // Create a temporary label to remember the insn for lineinfo.
1076 MCSymbol *LineSym = OS.getContext().createTempSymbol();
1077 OS.EmitLabel(LineSym);
1078
1079 // Construct the lineinfo.
1080 auto SP = DL.get()->getScope()->getSubprogram();
1081 constructLineInfo(SP, LineSym, DL.getLine(), DL.getCol());
1082
1083 LineInfoGenerated = true;
1084 PrevInstLoc = DL;
1085}
1086
Yonghong Songd3d88d02019-07-09 15:28:41 +00001087void BTFDebug::processGlobals(bool ProcessingMapDef) {
Yonghong Song7b410ac2018-12-19 16:40:25 +00001088 // Collect all types referenced by globals.
1089 const Module *M = MMI->getModule();
Yonghong Songcacac052019-03-15 05:51:25 +00001090 for (const GlobalVariable &Global : M->globals()) {
1091 // Ignore external globals for now.
Yonghong Song44ed2862019-03-15 17:39:10 +00001092 if (!Global.hasInitializer() && Global.hasExternalLinkage())
Yonghong Songcacac052019-03-15 05:51:25 +00001093 continue;
1094
Yonghong Songd3d88d02019-07-09 15:28:41 +00001095 // Decide the section name.
1096 StringRef SecName;
1097 if (Global.hasSection()) {
1098 SecName = Global.getSection();
1099 } else {
1100 // data, bss, or readonly sections
1101 if (Global.isConstant())
1102 SecName = ".rodata";
1103 else
1104 SecName = Global.getInitializer()->isZeroValue() ? ".bss" : ".data";
1105 }
1106
1107 if (ProcessingMapDef != SecName.startswith(".maps"))
1108 continue;
1109
Yonghong Songcacac052019-03-15 05:51:25 +00001110 SmallVector<DIGlobalVariableExpression *, 1> GVs;
1111 Global.getDebugInfo(GVs);
Yonghong Song6db6b562019-03-16 15:36:31 +00001112 uint32_t GVTypeId = 0;
Yonghong Songcacac052019-03-15 05:51:25 +00001113 for (auto *GVE : GVs) {
Yonghong Songd3d88d02019-07-09 15:28:41 +00001114 if (SecName.startswith(".maps"))
1115 visitMapDefType(GVE->getVariable()->getType(), GVTypeId);
1116 else
1117 visitTypeEntry(GVE->getVariable()->getType(), GVTypeId, false, false);
Yonghong Song6db6b562019-03-16 15:36:31 +00001118 break;
Yonghong Song7b410ac2018-12-19 16:40:25 +00001119 }
Yonghong Song6db6b562019-03-16 15:36:31 +00001120
1121 // Only support the following globals:
1122 // . static variables
1123 // . non-static global variables with section attributes
1124 // Essentially means:
1125 // . .bcc/.data/.rodata DataSec entities only contain static data
1126 // . Other DataSec entities contain static or initialized global data.
1127 // Initialized global data are mostly used for finding map key/value type
1128 // id's. Whether DataSec is readonly or not can be found from
1129 // corresponding ELF section flags.
1130 auto Linkage = Global.getLinkage();
1131 if (Linkage != GlobalValue::InternalLinkage &&
1132 (Linkage != GlobalValue::ExternalLinkage || !Global.hasSection()))
1133 continue;
1134
1135 uint32_t GVarInfo = Linkage == GlobalValue::ExternalLinkage
1136 ? BTF::VAR_GLOBAL_ALLOCATED
1137 : BTF::VAR_STATIC;
1138 auto VarEntry =
Jonas Devlieghere0eaee542019-08-15 15:54:37 +00001139 std::make_unique<BTFKindVar>(Global.getName(), GVTypeId, GVarInfo);
Yonghong Song6db6b562019-03-16 15:36:31 +00001140 uint32_t VarId = addType(std::move(VarEntry));
1141
Yonghong Song6db6b562019-03-16 15:36:31 +00001142 // Find or create a DataSec
1143 if (DataSecEntries.find(SecName) == DataSecEntries.end()) {
Jonas Devlieghere0eaee542019-08-15 15:54:37 +00001144 DataSecEntries[SecName] = std::make_unique<BTFKindDataSec>(Asm, SecName);
Yonghong Song6db6b562019-03-16 15:36:31 +00001145 }
1146
1147 // Calculate symbol size
1148 const DataLayout &DL = Global.getParent()->getDataLayout();
1149 uint32_t Size = DL.getTypeAllocSize(Global.getType()->getElementType());
1150
1151 DataSecEntries[SecName]->addVar(VarId, Asm->getSymbol(&Global), Size);
Yonghong Song7b410ac2018-12-19 16:40:25 +00001152 }
Yonghong Songd3d88d02019-07-09 15:28:41 +00001153}
Yonghong Song7b410ac2018-12-19 16:40:25 +00001154
Yonghong Songd3d88d02019-07-09 15:28:41 +00001155/// Emit proper patchable instructions.
1156bool BTFDebug::InstLower(const MachineInstr *MI, MCInst &OutMI) {
1157 if (MI->getOpcode() == BPF::LD_imm64) {
1158 const MachineOperand &MO = MI->getOperand(1);
1159 if (MO.isGlobal()) {
1160 const GlobalValue *GVal = MO.getGlobal();
1161 auto *GVar = dyn_cast<GlobalVariable>(GVal);
1162 if (GVar && GVar->hasAttribute(BPFCoreSharedInfo::AmaAttr)) {
1163 MDNode *MDN = GVar->getMetadata(LLVMContext::MD_preserve_access_index);
1164 DIType *Ty = dyn_cast<DIType>(MDN);
1165 std::string TypeName = Ty->getName();
Yonghong Songd8efec92019-07-25 16:01:26 +00001166 int64_t Imm = AccessOffsets[GVar->getName().str()];
Yonghong Songd3d88d02019-07-09 15:28:41 +00001167
1168 // Emit "mov ri, <imm>" for abstract member accesses.
1169 OutMI.setOpcode(BPF::MOV_ri);
1170 OutMI.addOperand(MCOperand::createReg(MI->getOperand(0).getReg()));
1171 OutMI.addOperand(MCOperand::createImm(Imm));
1172 return true;
1173 } else if (GVar && !GVar->hasInitializer() &&
1174 GVar->hasExternalLinkage() &&
1175 GVar->getSection() == BPFCoreSharedInfo::PatchableExtSecName) {
1176 const IntegerType *IntTy = dyn_cast<IntegerType>(GVar->getValueType());
1177 assert(IntTy);
1178 // For patchable externals, emit "LD_imm64, ri, 0" if the external
1179 // variable is 64bit width, emit "mov ri, 0" otherwise.
1180 if (IntTy->getBitWidth() == 64)
1181 OutMI.setOpcode(BPF::LD_imm64);
1182 else
1183 OutMI.setOpcode(BPF::MOV_ri);
1184 OutMI.addOperand(MCOperand::createReg(MI->getOperand(0).getReg()));
1185 OutMI.addOperand(MCOperand::createImm(0));
1186 return true;
1187 }
1188 }
1189 }
1190 return false;
Yonghong Song6db6b562019-03-16 15:36:31 +00001191}
1192
1193void BTFDebug::endModule() {
Yonghong Songd3d88d02019-07-09 15:28:41 +00001194 // Collect MapDef globals if not collected yet.
1195 if (MapDefNotCollected) {
1196 processGlobals(true);
1197 MapDefNotCollected = false;
1198 }
1199
1200 // Collect global types/variables except MapDef globals.
1201 processGlobals(false);
1202 for (auto &DataSec : DataSecEntries)
1203 addType(std::move(DataSec.second));
1204
1205 // Fixups
1206 for (auto &Fixup : FixupDerivedTypes) {
1207 StringRef TypeName = Fixup.first;
1208 bool IsUnion = Fixup.second.first;
1209
1210 // Search through struct types
1211 uint32_t StructTypeId = 0;
1212 for (const auto &StructType : StructTypes) {
1213 if (StructType->getName() == TypeName) {
1214 StructTypeId = StructType->getId();
1215 break;
1216 }
1217 }
1218
1219 if (StructTypeId == 0) {
Jonas Devlieghere0eaee542019-08-15 15:54:37 +00001220 auto FwdTypeEntry = std::make_unique<BTFTypeFwd>(TypeName, IsUnion);
Yonghong Songd3d88d02019-07-09 15:28:41 +00001221 StructTypeId = addType(std::move(FwdTypeEntry));
1222 }
1223
1224 for (auto &DType : Fixup.second.second) {
1225 DType->setPointeeType(StructTypeId);
1226 }
1227 }
Yonghong Song6db6b562019-03-16 15:36:31 +00001228
Yonghong Song7b410ac2018-12-19 16:40:25 +00001229 // Complete BTF type cross refereences.
1230 for (const auto &TypeEntry : TypeEntries)
1231 TypeEntry->completeType(*this);
1232
1233 // Emit BTF sections.
1234 emitBTFSection();
1235 emitBTFExtSection();
1236}