1//===- BTFParser.cpp ------------------------------------------------------===//
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// BTFParser reads/interprets .BTF and .BTF.ext ELF sections.
10// Refer to BTFParser.h for API description.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/DebugInfo/BTF/BTFParser.h"
15#include "llvm/ADT/StringExtras.h"
16#include "llvm/Support/Endian.h"
17#include "llvm/Support/Errc.h"
18#include "llvm/Support/FormatVariadic.h"
19
20#define DEBUG_TYPE "debug-info-btf-parser"
21
22using namespace llvm;
23using object::ObjectFile;
24using object::SectionedAddress;
25using object::SectionRef;
26
27const char BTFSectionName[] = ".BTF";
28const char BTFExtSectionName[] = ".BTF.ext";
29
30// Utility class with API similar to raw_ostream but can be cast
31// to Error, e.g.:
32//
33// Error foo(...) {
34// ...
35// if (Error E = bar(...))
36// return Err("error while foo(): ") << E;
37// ...
38// }
39//
40namespace {
41class Err {
42 std::string Buffer;
43 raw_string_ostream Stream;
44
45public:
46 Err(const char *InitialMsg) : Buffer(InitialMsg), Stream(Buffer) {}
47 Err(const char *SectionName, DataExtractor::Cursor &C)
48 : Buffer(), Stream(Buffer) {
49 *this << "error while reading " << SectionName
50 << " section: " << C.takeError();
51 };
52
53 template <typename T> Err &operator<<(T Val) {
54 Stream << Val;
55 return *this;
56 }
57
58 Err &write_hex(unsigned long long Val) {
59 Stream.write_hex(N: Val);
60 return *this;
61 }
62
63 Err &operator<<(Error Val) {
64 handleAllErrors(E: std::move(Val),
65 Handlers: [=](ErrorInfoBase &Info) { Stream << Info.message(); });
66 return *this;
67 }
68
69 operator Error() const {
70 return make_error<StringError>(Args: Buffer, Args: errc::invalid_argument);
71 }
72};
73} // anonymous namespace
74
75// ParseContext wraps information that is only necessary while parsing
76// ObjectFile and can be discarded once parsing is done.
77// Used by BTFParser::parse* auxiliary functions.
78struct BTFParser::ParseContext {
79 const ObjectFile &Obj;
80 const ParseOptions &Opts;
81 // Map from ELF section name to SectionRef
82 DenseMap<StringRef, SectionRef> Sections;
83
84public:
85 ParseContext(const ObjectFile &Obj, const ParseOptions &Opts)
86 : Obj(Obj), Opts(Opts) {}
87
88 Expected<DataExtractor> makeExtractor(SectionRef Sec) {
89 Expected<StringRef> Contents = Sec.getContents();
90 if (!Contents)
91 return Contents.takeError();
92 return DataExtractor(Contents.get(), Obj.isLittleEndian());
93 }
94
95 std::optional<SectionRef> findSection(StringRef Name) const {
96 auto It = Sections.find(Val: Name);
97 if (It != Sections.end())
98 return It->second;
99 return std::nullopt;
100 }
101};
102
103Error BTFParser::parseBTF(ParseContext &Ctx, SectionRef BTF) {
104 Expected<DataExtractor> MaybeExtractor = Ctx.makeExtractor(Sec: BTF);
105 if (!MaybeExtractor)
106 return MaybeExtractor.takeError();
107
108 DataExtractor &Extractor = MaybeExtractor.get();
109 DataExtractor::Cursor C = DataExtractor::Cursor(0);
110 uint16_t Magic = Extractor.getU16(C);
111 if (!C)
112 return Err(".BTF", C);
113 if (Magic != BTF::MAGIC)
114 return Err("invalid .BTF magic: ").write_hex(Val: Magic);
115 uint8_t Version = Extractor.getU8(C);
116 if (!C)
117 return Err(".BTF", C);
118 if (Version != 1)
119 return Err("unsupported .BTF version: ") << (unsigned)Version;
120 (void)Extractor.getU8(C); // flags
121 uint32_t HdrLen = Extractor.getU32(C);
122 if (!C)
123 return Err(".BTF", C);
124 if (HdrLen < 8)
125 return Err("unexpected .BTF header length: ") << HdrLen;
126 uint32_t TypeOff = Extractor.getU32(C);
127 uint32_t TypeLen = Extractor.getU32(C);
128 uint32_t StrOff = Extractor.getU32(C);
129 uint32_t StrLen = Extractor.getU32(C);
130 uint32_t StrStart = HdrLen + StrOff;
131 uint32_t StrEnd = StrStart + StrLen;
132 uint32_t TypesInfoStart = HdrLen + TypeOff;
133 uint32_t TypesInfoEnd = TypesInfoStart + TypeLen;
134 uint32_t BytesExpected = std::max(a: StrEnd, b: TypesInfoEnd);
135 if (!C)
136 return Err(".BTF", C);
137 if (Extractor.getData().size() < BytesExpected)
138 return Err("invalid .BTF section size, expecting at-least ")
139 << BytesExpected << " bytes";
140
141 StringsTable = Extractor.getData().slice(Start: StrStart, End: StrEnd);
142
143 if (TypeLen > 0 && Ctx.Opts.LoadTypes) {
144 StringRef RawData = Extractor.getData().slice(Start: TypesInfoStart, End: TypesInfoEnd);
145 if (Error E = parseTypesInfo(Ctx, TypesInfoStart, RawData))
146 return E;
147 }
148
149 return Error::success();
150}
151
152// Compute record size for each BTF::CommonType sub-type
153// (including entries in the tail position).
154static size_t byteSize(BTF::CommonType *Type) {
155 size_t Size = sizeof(BTF::CommonType);
156 switch (Type->getKind()) {
157 case BTF::BTF_KIND_INT:
158 Size += sizeof(uint32_t);
159 break;
160 case BTF::BTF_KIND_ARRAY:
161 Size += sizeof(BTF::BTFArray);
162 break;
163 case BTF::BTF_KIND_VAR:
164 Size += sizeof(uint32_t);
165 break;
166 case BTF::BTF_KIND_DECL_TAG:
167 Size += sizeof(uint32_t);
168 break;
169 case BTF::BTF_KIND_STRUCT:
170 case BTF::BTF_KIND_UNION:
171 Size += sizeof(BTF::BTFMember) * Type->getVlen();
172 break;
173 case BTF::BTF_KIND_ENUM:
174 Size += sizeof(BTF::BTFEnum) * Type->getVlen();
175 break;
176 case BTF::BTF_KIND_ENUM64:
177 Size += sizeof(BTF::BTFEnum64) * Type->getVlen();
178 break;
179 case BTF::BTF_KIND_FUNC_PROTO:
180 Size += sizeof(BTF::BTFParam) * Type->getVlen();
181 break;
182 case BTF::BTF_KIND_DATASEC:
183 Size += sizeof(BTF::BTFDataSec) * Type->getVlen();
184 break;
185 }
186 return Size;
187}
188
189// Guard value for voids, simplifies code a bit, but NameOff is not
190// actually valid.
191const BTF::CommonType VoidTypeInst = {.NameOff: 0, .Info: BTF::BTF_KIND_UNKN << 24, {.Size: 0}};
192
193// Type information "parsing" is very primitive:
194// - The `RawData` is copied to a buffer owned by `BTFParser` instance.
195// - The buffer is treated as an array of `uint32_t` values, each value
196// is swapped to use native endianness. This is possible, because
197// according to BTF spec all buffer elements are structures comprised
198// of `uint32_t` fields.
199// - `BTFParser::Types` vector is filled with pointers to buffer
200// elements, using `byteSize()` function to slice the buffer at type
201// record boundaries.
202// - If at some point a type definition with incorrect size (logical size
203// exceeding buffer boundaries) is reached it is not added to the
204// `BTFParser::Types` vector and the process stops.
205Error BTFParser::parseTypesInfo(ParseContext &Ctx, uint64_t TypesInfoStart,
206 StringRef RawData) {
207 using support::endian::byte_swap;
208
209 TypesBuffer.assign(AR: arrayRefFromStringRef(Input: RawData));
210 // Switch endianness if necessary.
211 endianness Endianness = Ctx.Obj.isLittleEndian() ? llvm::endianness::little
212 : llvm::endianness::big;
213 uint32_t *TypesBuffer32 = (uint32_t *)TypesBuffer.data();
214 for (uint64_t I = 0; I < TypesBuffer.size() / 4; ++I)
215 TypesBuffer32[I] = byte_swap(value: TypesBuffer32[I], endian: Endianness);
216
217 // The type id 0 is reserved for void type.
218 Types.push_back(x: &VoidTypeInst);
219
220 uint64_t Pos = 0;
221 while (Pos < RawData.size()) {
222 uint64_t BytesLeft = RawData.size() - Pos;
223 uint64_t Offset = TypesInfoStart + Pos;
224 BTF::CommonType *Type = (BTF::CommonType *)&TypesBuffer[Pos];
225 if (BytesLeft < sizeof(*Type))
226 return Err("incomplete type definition in .BTF section:")
227 << " offset " << Offset << ", index " << Types.size();
228
229 uint64_t Size = byteSize(Type);
230 if (BytesLeft < Size)
231 return Err("incomplete type definition in .BTF section:")
232 << " offset=" << Offset << ", index=" << Types.size()
233 << ", vlen=" << Type->getVlen();
234
235 LLVM_DEBUG({
236 llvm::dbgs() << "Adding BTF type:\n"
237 << " Id = " << Types.size() << "\n"
238 << " Kind = " << Type->getKind() << "\n"
239 << " Name = " << findString(Type->NameOff) << "\n"
240 << " Record Size = " << Size << "\n";
241 });
242 Types.push_back(x: Type);
243 Pos += Size;
244 }
245
246 return Error::success();
247}
248
249Error BTFParser::parseBTFExt(ParseContext &Ctx, SectionRef BTFExt) {
250 Expected<DataExtractor> MaybeExtractor = Ctx.makeExtractor(Sec: BTFExt);
251 if (!MaybeExtractor)
252 return MaybeExtractor.takeError();
253
254 DataExtractor &Extractor = MaybeExtractor.get();
255 DataExtractor::Cursor C = DataExtractor::Cursor(0);
256 uint16_t Magic = Extractor.getU16(C);
257 if (!C)
258 return Err(".BTF.ext", C);
259 if (Magic != BTF::MAGIC)
260 return Err("invalid .BTF.ext magic: ").write_hex(Val: Magic);
261 uint8_t Version = Extractor.getU8(C);
262 if (!C)
263 return Err(".BTF", C);
264 if (Version != 1)
265 return Err("unsupported .BTF.ext version: ") << (unsigned)Version;
266 (void)Extractor.getU8(C); // flags
267 uint32_t HdrLen = Extractor.getU32(C);
268 if (!C)
269 return Err(".BTF.ext", C);
270 if (HdrLen < 8)
271 return Err("unexpected .BTF.ext header length: ") << HdrLen;
272 (void)Extractor.getU32(C); // func_info_off
273 (void)Extractor.getU32(C); // func_info_len
274 uint32_t LineInfoOff = Extractor.getU32(C);
275 uint32_t LineInfoLen = Extractor.getU32(C);
276 uint32_t RelocInfoOff = Extractor.getU32(C);
277 uint32_t RelocInfoLen = Extractor.getU32(C);
278 if (!C)
279 return Err(".BTF.ext", C);
280
281 if (LineInfoLen > 0 && Ctx.Opts.LoadLines) {
282 uint32_t LineInfoStart = HdrLen + LineInfoOff;
283 uint32_t LineInfoEnd = LineInfoStart + LineInfoLen;
284 if (Error E = parseLineInfo(Ctx, Extractor, LineInfoStart, LineInfoEnd))
285 return E;
286 }
287
288 if (RelocInfoLen > 0 && Ctx.Opts.LoadRelocs) {
289 uint32_t RelocInfoStart = HdrLen + RelocInfoOff;
290 uint32_t RelocInfoEnd = RelocInfoStart + RelocInfoLen;
291 if (Error E = parseRelocInfo(Ctx, Extractor, RelocInfoStart, RelocInfoEnd))
292 return E;
293 }
294
295 return Error::success();
296}
297
298Error BTFParser::parseLineInfo(ParseContext &Ctx, DataExtractor &Extractor,
299 uint64_t LineInfoStart, uint64_t LineInfoEnd) {
300 DataExtractor::Cursor C = DataExtractor::Cursor(LineInfoStart);
301 uint32_t RecSize = Extractor.getU32(C);
302 if (!C)
303 return Err(".BTF.ext", C);
304 if (RecSize < 16)
305 return Err("unexpected .BTF.ext line info record length: ") << RecSize;
306
307 while (C && C.tell() < LineInfoEnd) {
308 uint32_t SecNameOff = Extractor.getU32(C);
309 uint32_t NumInfo = Extractor.getU32(C);
310 StringRef SecName = findString(Offset: SecNameOff);
311 std::optional<SectionRef> Sec = Ctx.findSection(Name: SecName);
312 if (!C)
313 return Err(".BTF.ext", C);
314 if (!Sec)
315 return Err("") << "can't find section '" << SecName
316 << "' while parsing .BTF.ext line info";
317 BTFLinesVector &Lines = SectionLines[Sec->getIndex()];
318 for (uint32_t I = 0; C && I < NumInfo; ++I) {
319 uint64_t RecStart = C.tell();
320 uint32_t InsnOff = Extractor.getU32(C);
321 uint32_t FileNameOff = Extractor.getU32(C);
322 uint32_t LineOff = Extractor.getU32(C);
323 uint32_t LineCol = Extractor.getU32(C);
324 if (!C)
325 return Err(".BTF.ext", C);
326 Lines.push_back(Elt: {.InsnOffset: InsnOff, .FileNameOff: FileNameOff, .LineOff: LineOff, .LineCol: LineCol});
327 C.seek(NewOffSet: RecStart + RecSize);
328 }
329 llvm::stable_sort(Range&: Lines,
330 C: [](const BTF::BPFLineInfo &L, const BTF::BPFLineInfo &R) {
331 return L.InsnOffset < R.InsnOffset;
332 });
333 }
334 if (!C)
335 return Err(".BTF.ext", C);
336
337 return Error::success();
338}
339
340Error BTFParser::parseRelocInfo(ParseContext &Ctx, DataExtractor &Extractor,
341 uint64_t RelocInfoStart,
342 uint64_t RelocInfoEnd) {
343 DataExtractor::Cursor C = DataExtractor::Cursor(RelocInfoStart);
344 uint32_t RecSize = Extractor.getU32(C);
345 if (!C)
346 return Err(".BTF.ext", C);
347 if (RecSize < 16)
348 return Err("unexpected .BTF.ext field reloc info record length: ")
349 << RecSize;
350 while (C && C.tell() < RelocInfoEnd) {
351 uint32_t SecNameOff = Extractor.getU32(C);
352 uint32_t NumInfo = Extractor.getU32(C);
353 StringRef SecName = findString(Offset: SecNameOff);
354 std::optional<SectionRef> Sec = Ctx.findSection(Name: SecName);
355 BTFRelocVector &Relocs = SectionRelocs[Sec->getIndex()];
356 for (uint32_t I = 0; C && I < NumInfo; ++I) {
357 uint64_t RecStart = C.tell();
358 uint32_t InsnOff = Extractor.getU32(C);
359 uint32_t TypeID = Extractor.getU32(C);
360 uint32_t OffsetNameOff = Extractor.getU32(C);
361 uint32_t RelocKind = Extractor.getU32(C);
362 if (!C)
363 return Err(".BTF.ext", C);
364 Relocs.push_back(Elt: {.InsnOffset: InsnOff, .TypeID: TypeID, .OffsetNameOff: OffsetNameOff, .RelocKind: RelocKind});
365 C.seek(NewOffSet: RecStart + RecSize);
366 }
367 llvm::stable_sort(
368 Range&: Relocs, C: [](const BTF::BPFFieldReloc &L, const BTF::BPFFieldReloc &R) {
369 return L.InsnOffset < R.InsnOffset;
370 });
371 }
372 if (!C)
373 return Err(".BTF.ext", C);
374
375 return Error::success();
376}
377
378Error BTFParser::parse(const ObjectFile &Obj, const ParseOptions &Opts) {
379 StringsTable = StringRef();
380 SectionLines.clear();
381 SectionRelocs.clear();
382 Types.clear();
383 TypesBuffer.clear();
384
385 ParseContext Ctx(Obj, Opts);
386 std::optional<SectionRef> BTF;
387 std::optional<SectionRef> BTFExt;
388 for (SectionRef Sec : Obj.sections()) {
389 Expected<StringRef> MaybeName = Sec.getName();
390 if (!MaybeName)
391 return Err("error while reading section name: ") << MaybeName.takeError();
392 Ctx.Sections[*MaybeName] = Sec;
393 if (*MaybeName == BTFSectionName)
394 BTF = Sec;
395 if (*MaybeName == BTFExtSectionName)
396 BTFExt = Sec;
397 }
398 if (!BTF)
399 return Err("can't find .BTF section");
400 if (!BTFExt)
401 return Err("can't find .BTF.ext section");
402 if (Error E = parseBTF(Ctx, BTF: *BTF))
403 return E;
404 if (Error E = parseBTFExt(Ctx, BTFExt: *BTFExt))
405 return E;
406
407 return Error::success();
408}
409
410bool BTFParser::hasBTFSections(const ObjectFile &Obj) {
411 bool HasBTF = false;
412 bool HasBTFExt = false;
413 for (SectionRef Sec : Obj.sections()) {
414 Expected<StringRef> Name = Sec.getName();
415 if (Error E = Name.takeError()) {
416 logAllUnhandledErrors(E: std::move(E), OS&: errs());
417 continue;
418 }
419 HasBTF |= *Name == BTFSectionName;
420 HasBTFExt |= *Name == BTFExtSectionName;
421 if (HasBTF && HasBTFExt)
422 return true;
423 }
424 return false;
425}
426
427StringRef BTFParser::findString(uint32_t Offset) const {
428 return StringsTable.slice(Start: Offset, End: StringsTable.find(C: 0, From: Offset));
429}
430
431template <typename T>
432static const T *findInfo(const DenseMap<uint64_t, SmallVector<T, 0>> &SecMap,
433 SectionedAddress Address) {
434 auto MaybeSecInfo = SecMap.find(Address.SectionIndex);
435 if (MaybeSecInfo == SecMap.end())
436 return nullptr;
437
438 const SmallVector<T, 0> &SecInfo = MaybeSecInfo->second;
439 const uint64_t TargetOffset = Address.Address;
440 typename SmallVector<T, 0>::const_iterator MaybeInfo = llvm::partition_point(
441 SecInfo, [=](const T &Entry) { return Entry.InsnOffset < TargetOffset; });
442 if (MaybeInfo == SecInfo.end() || MaybeInfo->InsnOffset != Address.Address)
443 return nullptr;
444
445 return &*MaybeInfo;
446}
447
448const BTF::BPFLineInfo *
449BTFParser::findLineInfo(SectionedAddress Address) const {
450 return findInfo(SecMap: SectionLines, Address);
451}
452
453const BTF::BPFFieldReloc *
454BTFParser::findFieldReloc(SectionedAddress Address) const {
455 return findInfo(SecMap: SectionRelocs, Address);
456}
457
458const BTF::CommonType *BTFParser::findType(uint32_t Id) const {
459 if (Id < Types.size())
460 return Types[Id];
461 return nullptr;
462}
463
464enum RelocKindGroup {
465 RKG_FIELD,
466 RKG_TYPE,
467 RKG_ENUMVAL,
468 RKG_UNKNOWN,
469};
470
471static RelocKindGroup relocKindGroup(const BTF::BPFFieldReloc *Reloc) {
472 switch (Reloc->RelocKind) {
473 case BTF::FIELD_BYTE_OFFSET:
474 case BTF::FIELD_BYTE_SIZE:
475 case BTF::FIELD_EXISTENCE:
476 case BTF::FIELD_SIGNEDNESS:
477 case BTF::FIELD_LSHIFT_U64:
478 case BTF::FIELD_RSHIFT_U64:
479 return RKG_FIELD;
480 case BTF::BTF_TYPE_ID_LOCAL:
481 case BTF::BTF_TYPE_ID_REMOTE:
482 case BTF::TYPE_EXISTENCE:
483 case BTF::TYPE_MATCH:
484 case BTF::TYPE_SIZE:
485 return RKG_TYPE;
486 case BTF::ENUM_VALUE_EXISTENCE:
487 case BTF::ENUM_VALUE:
488 return RKG_ENUMVAL;
489 default:
490 return RKG_UNKNOWN;
491 }
492}
493
494static bool isMod(const BTF::CommonType *Type) {
495 switch (Type->getKind()) {
496 case BTF::BTF_KIND_VOLATILE:
497 case BTF::BTF_KIND_CONST:
498 case BTF::BTF_KIND_RESTRICT:
499 case BTF::BTF_KIND_TYPE_TAG:
500 return true;
501 default:
502 return false;
503 }
504}
505
506static bool printMod(const BTFParser &BTF, const BTF::CommonType *Type,
507 raw_ostream &Stream) {
508 switch (Type->getKind()) {
509 case BTF::BTF_KIND_CONST:
510 Stream << " const";
511 break;
512 case BTF::BTF_KIND_VOLATILE:
513 Stream << " volatile";
514 break;
515 case BTF::BTF_KIND_RESTRICT:
516 Stream << " restrict";
517 break;
518 case BTF::BTF_KIND_TYPE_TAG:
519 Stream << " type_tag(\"" << BTF.findString(Offset: Type->NameOff) << "\")";
520 break;
521 default:
522 return false;
523 }
524 return true;
525}
526
527static const BTF::CommonType *skipModsAndTypedefs(const BTFParser &BTF,
528 const BTF::CommonType *Type) {
529 while (isMod(Type) || Type->getKind() == BTF::BTF_KIND_TYPEDEF) {
530 auto *Base = BTF.findType(Id: Type->Type);
531 if (!Base)
532 break;
533 Type = Base;
534 }
535 return Type;
536}
537
538namespace {
539struct StrOrAnon {
540 const BTFParser &BTF;
541 uint32_t Offset;
542 uint32_t Idx;
543};
544
545static raw_ostream &operator<<(raw_ostream &Stream, const StrOrAnon &S) {
546 StringRef Str = S.BTF.findString(Offset: S.Offset);
547 if (Str.empty())
548 Stream << "<anon " << S.Idx << ">";
549 else
550 Stream << Str;
551 return Stream;
552}
553} // anonymous namespace
554
555static void relocKindName(uint32_t X, raw_ostream &Out) {
556 Out << "<";
557 switch (X) {
558 default:
559 Out << "reloc kind #" << X;
560 break;
561 case BTF::FIELD_BYTE_OFFSET:
562 Out << "byte_off";
563 break;
564 case BTF::FIELD_BYTE_SIZE:
565 Out << "byte_sz";
566 break;
567 case BTF::FIELD_EXISTENCE:
568 Out << "field_exists";
569 break;
570 case BTF::FIELD_SIGNEDNESS:
571 Out << "signed";
572 break;
573 case BTF::FIELD_LSHIFT_U64:
574 Out << "lshift_u64";
575 break;
576 case BTF::FIELD_RSHIFT_U64:
577 Out << "rshift_u64";
578 break;
579 case BTF::BTF_TYPE_ID_LOCAL:
580 Out << "local_type_id";
581 break;
582 case BTF::BTF_TYPE_ID_REMOTE:
583 Out << "target_type_id";
584 break;
585 case BTF::TYPE_EXISTENCE:
586 Out << "type_exists";
587 break;
588 case BTF::TYPE_MATCH:
589 Out << "type_matches";
590 break;
591 case BTF::TYPE_SIZE:
592 Out << "type_size";
593 break;
594 case BTF::ENUM_VALUE_EXISTENCE:
595 Out << "enumval_exists";
596 break;
597 case BTF::ENUM_VALUE:
598 Out << "enumval_value";
599 break;
600 }
601 Out << ">";
602}
603
604// Produces a human readable description of a CO-RE relocation.
605// Such relocations are generated by BPF backend, and processed
606// by libbpf's BPF program loader [1].
607//
608// Each relocation record has the following information:
609// - Relocation kind;
610// - BTF type ID;
611// - Access string offset in string table.
612//
613// There are different kinds of relocations, these kinds could be split
614// in three groups:
615// - load-time information about types (size, existence),
616// `BTFParser::symbolize()` output for such relocations uses the template:
617//
618// <relocation-kind> [<id>] <type-name>
619//
620// For example:
621// - "<type_exists> [7] struct foo"
622// - "<type_size> [7] struct foo"
623//
624// - load-time information about enums (literal existence, literal value),
625// `BTFParser::symbolize()` output for such relocations uses the template:
626//
627// <relocation-kind> [<id>] <type-name>::<literal-name> = <original-value>
628//
629// For example:
630// - "<enumval_exists> [5] enum foo::U = 1"
631// - "<enumval_value> [5] enum foo::V = 2"
632//
633// - load-time information about fields (e.g. field offset),
634// `BTFParser::symbolize()` output for such relocations uses the template:
635//
636// <relocation-kind> [<id>] \
637// <type-name>::[N].<field-1-name>...<field-M-name> \
638// (<access string>)
639//
640// For example:
641// - "<byte_off> [8] struct bar::[7].v (7:1)"
642// - "<field_exists> [8] struct bar::v (0:1)"
643//
644// If relocation description is not valid output follows the following pattern:
645//
646// <relocation-kind> <type-id>::<unprocessedaccess-string> <<error-msg>>
647//
648// For example:
649//
650// - "<type_sz> [42] '' <unknown type id: 42>"
651// - "<byte_off> [4] '0:' <field spec too short>"
652//
653// Additional examples could be found in unit tests, see
654// llvm/unittests/DebugInfo/BTF/BTFParserTest.cpp.
655//
656// [1] https://www.kernel.org/doc/html/latest/bpf/libbpf/index.html
657void BTFParser::symbolize(const BTF::BPFFieldReloc *Reloc,
658 SmallVectorImpl<char> &Result) const {
659 raw_svector_ostream Stream(Result);
660 StringRef FullSpecStr = findString(Offset: Reloc->OffsetNameOff);
661 SmallVector<uint32_t, 8> RawSpec;
662
663 auto Fail = [&](auto Msg) {
664 Result.resize(N: 0);
665 relocKindName(X: Reloc->RelocKind, Out&: Stream);
666 Stream << " [" << Reloc->TypeID << "] '" << FullSpecStr << "'"
667 << " <" << Msg << ">";
668 };
669
670 // Relocation access string follows pattern [0-9]+(:[0-9]+)*,
671 // e.g.: 12:22:3. Code below splits `SpecStr` by ':', parses
672 // numbers, and pushes them to `RawSpec`.
673 StringRef SpecStr = FullSpecStr;
674 while (SpecStr.size()) {
675 unsigned long long Val;
676 if (consumeUnsignedInteger(Str&: SpecStr, Radix: 10, Result&: Val))
677 return Fail("spec string is not a number");
678 RawSpec.push_back(Elt: Val);
679 if (SpecStr.empty())
680 break;
681 if (SpecStr[0] != ':')
682 return Fail(
683 formatv(Fmt: "unexpected spec string delimiter: '{0}'", Vals: SpecStr[0]));
684 SpecStr = SpecStr.substr(Start: 1);
685 }
686
687 // Print relocation kind to `Stream`.
688 relocKindName(X: Reloc->RelocKind, Out&: Stream);
689
690 uint32_t CurId = Reloc->TypeID;
691 const BTF::CommonType *Type = findType(Id: CurId);
692 if (!Type)
693 return Fail(formatv(Fmt: "unknown type id: {0:d}", Vals&: CurId));
694
695 Stream << " [" << CurId << "]";
696
697 // `Type` might have modifiers, e.g. for type 'const int' the `Type`
698 // would refer to BTF type of kind BTF_KIND_CONST.
699 // Print all these modifiers to `Stream`.
700 for (uint32_t ChainLen = 0; printMod(BTF: *this, Type, Stream); ++ChainLen) {
701 if (ChainLen >= 32)
702 return Fail("modifiers chain is too long");
703
704 CurId = Type->Type;
705 const BTF::CommonType *NextType = findType(Id: CurId);
706 if (!NextType)
707 return Fail(formatv(Fmt: "unknown type id: {0:d} in modifiers chain", Vals&: CurId));
708 Type = NextType;
709 }
710 // Print the type name to `Stream`.
711 if (CurId == 0) {
712 Stream << " void";
713 } else {
714 switch (Type->getKind()) {
715 case BTF::BTF_KIND_TYPEDEF:
716 Stream << " typedef";
717 break;
718 case BTF::BTF_KIND_STRUCT:
719 Stream << " struct";
720 break;
721 case BTF::BTF_KIND_UNION:
722 Stream << " union";
723 break;
724 case BTF::BTF_KIND_ENUM:
725 Stream << " enum";
726 break;
727 case BTF::BTF_KIND_ENUM64:
728 Stream << " enum";
729 break;
730 case BTF::BTF_KIND_FWD:
731 if (Type->Info & BTF::FWD_UNION_FLAG)
732 Stream << " fwd union";
733 else
734 Stream << " fwd struct";
735 break;
736 default:
737 break;
738 }
739 Stream << " " << StrOrAnon({.BTF: *this, .Offset: Type->NameOff, .Idx: CurId});
740 }
741
742 RelocKindGroup Group = relocKindGroup(Reloc);
743 // Type-based relocations don't use access string but clang backend
744 // generates '0' and libbpf checks it's value, do the same here.
745 if (Group == RKG_TYPE) {
746 if (RawSpec.size() != 1 || RawSpec[0] != 0)
747 return Fail("unexpected type-based relocation spec: should be '0'");
748 return;
749 }
750
751 Stream << "::";
752
753 // For enum-based relocations access string is a single number,
754 // corresponding to the enum literal sequential number.
755 // E.g. for `enum E { U, V }`, relocation requesting value of `V`
756 // would look as follows:
757 // - kind: BTF::ENUM_VALUE
758 // - BTF id: id for `E`
759 // - access string: "1"
760 if (Group == RKG_ENUMVAL) {
761 Type = skipModsAndTypedefs(BTF: *this, Type);
762
763 if (RawSpec.size() != 1)
764 return Fail("unexpected enumval relocation spec size");
765
766 uint32_t NameOff;
767 uint64_t Val;
768 uint32_t Idx = RawSpec[0];
769 if (auto *T = dyn_cast<BTF::EnumType>(Val: Type)) {
770 if (T->values().size() <= Idx)
771 return Fail(formatv(Fmt: "bad value index: {0:d}", Vals&: Idx));
772 const BTF::BTFEnum &E = T->values()[Idx];
773 NameOff = E.NameOff;
774 Val = E.Val;
775 } else if (auto *T = dyn_cast<BTF::Enum64Type>(Val: Type)) {
776 if (T->values().size() <= Idx)
777 return Fail(formatv(Fmt: "bad value index: {0:d}", Vals&: Idx));
778 const BTF::BTFEnum64 &E = T->values()[Idx];
779 NameOff = E.NameOff;
780 Val = (uint64_t)E.Val_Hi32 << 32u | E.Val_Lo32;
781 } else {
782 return Fail(formatv(Fmt: "unexpected type kind for enum relocation: {0:d}",
783 Vals: Type->getKind()));
784 }
785
786 Stream << StrOrAnon({.BTF: *this, .Offset: NameOff, .Idx: Idx});
787 if (Type->Info & BTF::ENUM_SIGNED_FLAG)
788 Stream << " = " << (int64_t)Val;
789 else
790 Stream << " = " << (uint64_t)Val;
791 return;
792 }
793
794 // For type-based relocations access string is an array of numbers,
795 // which resemble index parameters for `getelementptr` LLVM IR instruction.
796 // E.g. for the following types:
797 //
798 // struct foo {
799 // int a;
800 // int b;
801 // };
802 // struct bar {
803 // int u;
804 // struct foo v[7];
805 // };
806 //
807 // Relocation requesting `offsetof(struct bar, v[2].b)` will have
808 // the following access string: 0:1:2:1
809 // ^ ^ ^ ^
810 // | | | |
811 // initial index | | field 'b' is a field #1
812 // | | (counting from 0)
813 // | array index #2
814 // field 'v' is a field #1
815 // (counting from 0)
816 if (Group == RKG_FIELD) {
817 if (RawSpec.size() < 1)
818 return Fail("field spec too short");
819
820 if (RawSpec[0] != 0)
821 Stream << "[" << RawSpec[0] << "]";
822 for (uint32_t I = 1; I < RawSpec.size(); ++I) {
823 Type = skipModsAndTypedefs(BTF: *this, Type);
824 uint32_t Idx = RawSpec[I];
825
826 if (auto *T = dyn_cast<BTF::StructType>(Val: Type)) {
827 if (T->getVlen() <= Idx)
828 return Fail(formatv(
829 Fmt: "member index {0:d} for spec sub-string {1:d} is out of range",
830 Vals&: Idx, Vals&: I));
831
832 const BTF::BTFMember &Member = T->members()[Idx];
833 if (I != 1 || RawSpec[0] != 0)
834 Stream << ".";
835 Stream << StrOrAnon({.BTF: *this, .Offset: Member.NameOff, .Idx: Idx});
836 Type = findType(Id: Member.Type);
837 if (!Type)
838 return Fail(
839 formatv(Fmt: "unknown member type id {0:d} for spec sub-string {1:d}",
840 Vals: Member.Type, Vals&: I));
841 } else if (auto *T = dyn_cast<BTF::ArrayType>(Val: Type)) {
842 Stream << "[" << Idx << "]";
843 Type = findType(Id: T->getArray().ElemType);
844 if (!Type)
845 return Fail(
846 formatv(Fmt: "unknown element type id {0:d} for spec sub-string {1:d}",
847 Vals: T->getArray().ElemType, Vals&: I));
848 } else {
849 return Fail(
850 formatv(Fmt: "unexpected type kind {0:d} for spec sub-string {1:d}",
851 Vals: Type->getKind(), Vals&: I));
852 }
853 }
854
855 Stream << " (" << FullSpecStr << ")";
856 return;
857 }
858
859 return Fail(formatv(Fmt: "unknown relocation kind: {0:d}", Vals: Reloc->RelocKind));
860}
861