1//===- BTFDebug.h -----------------------------------------------*- C++ -*-===//
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/// \file
10/// This file contains support for writing BTF debug info.
11///
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_LIB_TARGET_BPF_BTFDEBUG_H
15#define LLVM_LIB_TARGET_BPF_BTFDEBUG_H
16
17#include "llvm/ADT/ArrayRef.h"
18#include "llvm/ADT/DenseMap.h"
19#include "llvm/ADT/StringMap.h"
20#include "llvm/CodeGen/DebugHandlerBase.h"
21#include "llvm/DebugInfo/BTF/BTF.h"
22#include <cstdint>
23#include <map>
24#include <set>
25
26namespace llvm {
27
28class AsmPrinter;
29class BTFDebug;
30class DIType;
31class GlobalVariable;
32class MachineFunction;
33class MachineInstr;
34class MachineOperand;
35class MCInst;
36class MCStreamer;
37class MCSymbol;
38
39/// The base class for BTF type generation.
40class BTFTypeBase {
41protected:
42 uint8_t Kind;
43 bool IsCompleted;
44 uint32_t Id;
45 struct BTF::CommonType BTFType;
46
47public:
48 BTFTypeBase() : IsCompleted(false) {}
49 virtual ~BTFTypeBase() = default;
50 void setId(uint32_t Id) { this->Id = Id; }
51 uint32_t getId() { return Id; }
52 uint32_t roundupToBytes(uint32_t NumBits) { return (NumBits + 7) >> 3; }
53 /// Get the size of this BTF type entry.
54 virtual uint32_t getSize() { return BTF::CommonTypeSize; }
55 /// Complete BTF type generation after all related DebugInfo types
56 /// have been visited so their BTF type id's are available
57 /// for cross referece.
58 virtual void completeType(BTFDebug &BDebug) {}
59 /// Emit types for this BTF type entry.
60 virtual void emitType(MCStreamer &OS);
61};
62
63/// Handle several derived types include pointer, const,
64/// volatile, typedef and restrict.
65class BTFTypeDerived : public BTFTypeBase {
66 const DIDerivedType *DTy;
67 bool NeedsFixup;
68 StringRef Name;
69
70public:
71 BTFTypeDerived(const DIDerivedType *Ty, unsigned Tag, bool NeedsFixup);
72 BTFTypeDerived(unsigned NextTypeId, unsigned Tag, StringRef Name);
73 void completeType(BTFDebug &BDebug) override;
74 void emitType(MCStreamer &OS) override;
75 void setPointeeType(uint32_t PointeeType);
76};
77
78/// Handle struct or union forward declaration.
79class BTFTypeFwd : public BTFTypeBase {
80 StringRef Name;
81
82public:
83 BTFTypeFwd(StringRef Name, bool IsUnion);
84 void completeType(BTFDebug &BDebug) override;
85 void emitType(MCStreamer &OS) override;
86};
87
88/// Handle int type.
89class BTFTypeInt : public BTFTypeBase {
90 StringRef Name;
91 uint32_t IntVal; ///< Encoding, offset, bits
92
93public:
94 BTFTypeInt(uint32_t Encoding, uint32_t SizeInBits, uint32_t OffsetInBits,
95 StringRef TypeName);
96 uint32_t getSize() override { return BTFTypeBase::getSize() + sizeof(uint32_t); }
97 void completeType(BTFDebug &BDebug) override;
98 void emitType(MCStreamer &OS) override;
99};
100
101/// Handle enumerate type.
102class BTFTypeEnum : public BTFTypeBase {
103 const DICompositeType *ETy;
104 std::vector<struct BTF::BTFEnum> EnumValues;
105
106public:
107 BTFTypeEnum(const DICompositeType *ETy, uint32_t NumValues, bool IsSigned);
108 uint32_t getSize() override {
109 return BTFTypeBase::getSize() + EnumValues.size() * BTF::BTFEnumSize;
110 }
111 void completeType(BTFDebug &BDebug) override;
112 void emitType(MCStreamer &OS) override;
113};
114
115/// Handle array type.
116class BTFTypeArray : public BTFTypeBase {
117 struct BTF::BTFArray ArrayInfo;
118
119public:
120 BTFTypeArray(uint32_t ElemTypeId, uint32_t NumElems);
121 uint32_t getSize() override { return BTFTypeBase::getSize() + BTF::BTFArraySize; }
122 void completeType(BTFDebug &BDebug) override;
123 void emitType(MCStreamer &OS) override;
124};
125
126/// Handle struct/union type.
127class BTFTypeStruct : public BTFTypeBase {
128 const DICompositeType *STy;
129 std::vector<const DINode *> Elements;
130 bool HasBitField;
131 std::vector<struct BTF::BTFMember> Members;
132
133public:
134 BTFTypeStruct(const DICompositeType *STy, ArrayRef<const DINode *> Elements,
135 bool IsStruct, bool HasBitField, uint32_t NumMembers);
136 uint32_t getSize() override {
137 return BTFTypeBase::getSize() + Members.size() * BTF::BTFMemberSize;
138 }
139 void completeType(BTFDebug &BDebug) override;
140 void emitType(MCStreamer &OS) override;
141 std::string getName();
142};
143
144/// Handle function pointer.
145class BTFTypeFuncProto : public BTFTypeBase {
146 const DISubroutineType *STy;
147 SmallDenseMap<uint32_t, StringRef> FuncArgNames;
148 SmallVector<uint32_t, 8> AliveParamIndices;
149 bool UseFilteredParams = false;
150 std::vector<struct BTF::BTFParam> Parameters;
151 bool VoidReturn = false;
152
153public:
154 BTFTypeFuncProto(const DISubroutineType *STy, uint32_t NumParams,
155 const SmallDenseMap<uint32_t, StringRef> &FuncArgNames,
156 bool UseFilteredParams = false,
157 ArrayRef<uint32_t> AliveParamIndices = {},
158 bool VoidReturn = false);
159 uint32_t getSize() override {
160 return BTFTypeBase::getSize() + Parameters.size() * BTF::BTFParamSize;
161 }
162 void completeType(BTFDebug &BDebug) override;
163 void emitType(MCStreamer &OS) override;
164};
165
166/// Handle subprogram
167class BTFTypeFunc : public BTFTypeBase {
168 StringRef Name;
169
170public:
171 BTFTypeFunc(StringRef FuncName, uint32_t ProtoTypeId, uint32_t Scope);
172 uint32_t getSize() override { return BTFTypeBase::getSize(); }
173 void completeType(BTFDebug &BDebug) override;
174 void emitType(MCStreamer &OS) override;
175};
176
177/// Handle variable instances
178class BTFKindVar : public BTFTypeBase {
179 StringRef Name;
180 uint32_t Info;
181
182public:
183 BTFKindVar(StringRef VarName, uint32_t TypeId, uint32_t VarInfo);
184 uint32_t getSize() override { return BTFTypeBase::getSize() + 4; }
185 void completeType(BTFDebug &BDebug) override;
186 void emitType(MCStreamer &OS) override;
187};
188
189/// Handle data sections
190class BTFKindDataSec : public BTFTypeBase {
191 AsmPrinter *Asm;
192 std::string Name;
193 std::vector<std::tuple<uint32_t, const MCSymbol *, uint32_t>> Vars;
194
195public:
196 BTFKindDataSec(AsmPrinter *AsmPrt, std::string SecName);
197 uint32_t getSize() override {
198 return BTFTypeBase::getSize() + BTF::BTFDataSecVarSize * Vars.size();
199 }
200 void addDataSecEntry(uint32_t Id, const MCSymbol *Sym, uint32_t Size) {
201 Vars.push_back(x: std::make_tuple(args&: Id, args&: Sym, args&: Size));
202 }
203 std::string getName() { return Name; }
204 void completeType(BTFDebug &BDebug) override;
205 void emitType(MCStreamer &OS) override;
206};
207
208/// Handle binary floating point type.
209class BTFTypeFloat : public BTFTypeBase {
210 StringRef Name;
211
212public:
213 BTFTypeFloat(uint32_t SizeInBits, StringRef TypeName);
214 void completeType(BTFDebug &BDebug) override;
215};
216
217/// Handle decl tags.
218class BTFTypeDeclTag : public BTFTypeBase {
219 uint32_t Info;
220 StringRef Tag;
221
222public:
223 BTFTypeDeclTag(uint32_t BaseTypeId, int ComponentId, StringRef Tag);
224 uint32_t getSize() override { return BTFTypeBase::getSize() + 4; }
225 void completeType(BTFDebug &BDebug) override;
226 void emitType(MCStreamer &OS) override;
227};
228
229/// Handle 64-bit enumerate type.
230class BTFTypeEnum64 : public BTFTypeBase {
231 const DICompositeType *ETy;
232 std::vector<struct BTF::BTFEnum64> EnumValues;
233
234public:
235 BTFTypeEnum64(const DICompositeType *ETy, uint32_t NumValues, bool IsSigned);
236 uint32_t getSize() override {
237 return BTFTypeBase::getSize() + EnumValues.size() * BTF::BTFEnum64Size;
238 }
239 void completeType(BTFDebug &BDebug) override;
240 void emitType(MCStreamer &OS) override;
241};
242
243class BTFTypeTypeTag : public BTFTypeBase {
244 const DIDerivedType *DTy;
245 StringRef Tag;
246
247public:
248 BTFTypeTypeTag(uint32_t NextTypeId, StringRef Tag);
249 BTFTypeTypeTag(const DIDerivedType *DTy, StringRef Tag);
250 void completeType(BTFDebug &BDebug) override;
251};
252
253/// String table.
254class BTFStringTable {
255 /// String table size in bytes.
256 uint32_t Size;
257 /// A mapping from string table offset to the index
258 /// of the Table. It is used to avoid putting
259 /// duplicated strings in the table.
260 std::map<uint32_t, uint32_t> OffsetToIdMap;
261 /// A vector of strings to represent the string table.
262 std::vector<std::string> Table;
263
264public:
265 BTFStringTable() : Size(0) {}
266 uint32_t getSize() { return Size; }
267 std::vector<std::string> &getTable() { return Table; }
268 /// Add a string to the string table and returns its offset
269 /// in the table.
270 uint32_t addString(StringRef S);
271};
272
273/// Represent one func and its type id.
274struct BTFFuncInfo {
275 const MCSymbol *Label; ///< Func MCSymbol
276 uint32_t TypeId; ///< Type id referring to .BTF type section
277};
278
279/// Represent one line info.
280struct BTFLineInfo {
281 MCSymbol *Label; ///< MCSymbol identifying insn for the lineinfo
282 uint32_t FileNameOff; ///< file name offset in the .BTF string table
283 uint32_t LineOff; ///< line offset in the .BTF string table
284 uint32_t LineNum; ///< the line number
285 uint32_t ColumnNum; ///< the column number
286};
287
288/// Represent one field relocation.
289struct BTFFieldReloc {
290 const MCSymbol *Label; ///< MCSymbol identifying insn for the reloc
291 uint32_t TypeID; ///< Type ID
292 uint32_t OffsetNameOff; ///< The string to traverse types
293 uint32_t RelocKind; ///< What to patch the instruction
294};
295
296/// Collect and emit BTF information.
297class BTFDebug : public DebugHandlerBase {
298 MCStreamer &OS;
299 bool SkipInstruction;
300 bool LineInfoGenerated;
301 uint32_t SecNameOff;
302 uint32_t ArrayIndexTypeId;
303 bool MapDefNotCollected;
304 BTFStringTable StringTable;
305 std::vector<std::unique_ptr<BTFTypeBase>> TypeEntries;
306 DenseMap<const DIType *, uint32_t> DIToIdMap;
307 std::map<uint32_t, std::vector<BTFFuncInfo>> FuncInfoTable;
308 std::map<uint32_t, std::vector<BTFLineInfo>> LineInfoTable;
309 std::map<uint32_t, std::vector<BTFFieldReloc>> FieldRelocTable;
310 StringMap<std::vector<std::string>> FileContent;
311 std::map<std::string, std::unique_ptr<BTFKindDataSec>, std::less<>>
312 DataSecEntries;
313 std::vector<BTFTypeStruct *> StructTypes;
314 std::map<const GlobalVariable *, std::pair<int64_t, uint32_t>> PatchImms;
315 std::map<const DICompositeType *,
316 std::vector<std::pair<const DIDerivedType *, BTFTypeDerived *>>>
317 FixupDerivedTypes;
318 std::set<const Function *>ProtoFunctions;
319
320 /// Add types to TypeEntries.
321 /// @{
322 /// Add types to TypeEntries and DIToIdMap.
323 uint32_t addType(std::unique_ptr<BTFTypeBase> TypeEntry, const DIType *Ty);
324 /// Add types to TypeEntries only and return type id.
325 uint32_t addType(std::unique_ptr<BTFTypeBase> TypeEntry);
326 /// @}
327
328 /// IR type visiting functions.
329 /// @{
330 void visitTypeEntry(const DIType *Ty);
331 void visitTypeEntry(const DIType *Ty, uint32_t &TypeId, bool CheckPointer,
332 bool SeenPointer);
333 void visitBasicType(const DIBasicType *BTy, uint32_t &TypeId);
334 void
335 visitSubroutineType(const DISubroutineType *STy, bool ForSubprog,
336 const SmallDenseMap<uint32_t, StringRef> &FuncArgNames,
337 uint32_t &TypeId, bool VoidReturn = false);
338 void visitFwdDeclType(const DICompositeType *CTy, bool IsUnion,
339 uint32_t &TypeId);
340 void visitCompositeType(const DICompositeType *CTy, uint32_t &TypeId);
341 void visitStructType(const DICompositeType *STy, bool IsStruct,
342 uint32_t &TypeId);
343 void visitArrayType(const DICompositeType *ATy, uint32_t &TypeId);
344 void visitEnumType(const DICompositeType *ETy, uint32_t &TypeId);
345 void visitDerivedType(const DIDerivedType *DTy, uint32_t &TypeId,
346 bool CheckPointer, bool SeenPointer);
347 void visitMapDefType(const DIType *Ty, uint32_t &TypeId);
348 /// @}
349
350 /// Check whether the type is a forward declaration candidate or not.
351 bool IsForwardDeclCandidate(const DIType *Base);
352
353 /// Get the file content for the subprogram. Certain lines of the file
354 /// later may be put into string table and referenced by line info.
355 std::string populateFileContent(const DIFile *File);
356
357 /// Construct a line info.
358 void constructLineInfo(MCSymbol *Label, const DIFile *File, uint32_t Line,
359 uint32_t Column);
360
361 /// Generate types and variables for globals.
362 void processGlobals(bool ProcessingMapDef);
363
364 /// Process global variable initializer in pursuit for function
365 /// pointers.
366 void processGlobalInitializer(const Constant *C);
367
368 /// Generate types for function prototypes.
369 void processFuncPrototypes(const Function *);
370
371 /// Generate types for decl annotations.
372 void processDeclAnnotations(DINodeArray Annotations, uint32_t BaseTypeId,
373 int ComponentId);
374
375 /// Generate types for DISubprogram and it's arguments.
376 uint32_t processDISubprogram(
377 const DISubprogram *SP, uint32_t ProtoTypeId, uint8_t Scope,
378 const SmallDenseMap<uint32_t, uint32_t> *ArgIndexMap = nullptr);
379
380 /// Generate BTF type_tag's. If BaseTypeId is nonnegative, the last
381 /// BTF type_tag in the chain points to BaseTypeId. Otherwise, it points to
382 /// the base type of DTy. Return the type id of the first BTF type_tag
383 /// in the chain. If no type_tag's are generated, a negative value
384 /// is returned.
385 int genBTFTypeTags(const DIDerivedType *DTy, int BaseTypeId);
386
387 /// Generate one field relocation record.
388 void generatePatchImmReloc(const MCSymbol *ORSym, uint32_t RootId,
389 const GlobalVariable *, bool IsAma);
390
391 /// Populating unprocessed type on demand.
392 unsigned populateType(const DIType *Ty);
393
394 /// Process global variables referenced by relocation instructions
395 /// and extern function references.
396 void processGlobalValue(const MachineOperand &MO);
397
398 /// Emit common header of .BTF and .BTF.ext sections.
399 void emitCommonHeader();
400
401 /// Emit the .BTF section.
402 void emitBTFSection();
403
404 /// Emit the .BTF.ext section.
405 void emitBTFExtSection();
406
407protected:
408 /// Gather pre-function debug information.
409 void beginFunctionImpl(const MachineFunction *MF) override;
410
411 /// Post process after all instructions in this function are processed.
412 void endFunctionImpl(const MachineFunction *MF) override;
413
414public:
415 BTFDebug(AsmPrinter *AP);
416
417 ///
418 bool InstLower(const MachineInstr *MI, MCInst &OutMI);
419
420 /// Get the special array index type id.
421 uint32_t getArrayIndexTypeId() {
422 assert(ArrayIndexTypeId);
423 return ArrayIndexTypeId;
424 }
425
426 /// Add string to the string table.
427 size_t addString(StringRef S) { return StringTable.addString(S); }
428
429 /// Get the type id for a particular DIType.
430 uint32_t getTypeId(const DIType *Ty) {
431 assert(Ty && "Invalid null Type");
432 assert(DIToIdMap.find(Ty) != DIToIdMap.end() &&
433 "DIType not added in the BDIToIdMap");
434 return DIToIdMap[Ty];
435 }
436
437 /// Process beginning of an instruction.
438 void beginInstruction(const MachineInstr *MI) override;
439
440 /// Complete all the types and emit the BTF sections.
441 void endModule() override;
442};
443
444} // end namespace llvm
445
446#endif
447