LLVM 22.0.0git
BTFParser.h
Go to the documentation of this file.
1//===- BTFParser.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// BTFParser reads .BTF and .BTF.ext ELF sections generated by LLVM
10// BPF backend and provides introspection for the stored information.
11// Currently the following information is accessible:
12// - string table;
13// - instruction offset to line information mapping;
14// - types table;
15// - CO-RE relocations table.
16//
17// See llvm/DebugInfo/BTF/BTF.h for some details about binary format
18// and links to Linux Kernel documentation.
19//
20//===----------------------------------------------------------------------===//
21
22#ifndef LLVM_DEBUGINFO_BTF_BTFPARSER_H
23#define LLVM_DEBUGINFO_BTF_BTFPARSER_H
24
25#include "llvm/ADT/DenseMap.h"
30
31namespace llvm {
32using object::ObjectFile;
33using object::SectionedAddress;
34using object::SectionRef;
35
36class BTFParser {
39
40 // In BTF strings are stored as a continuous memory region with
41 // individual strings separated by 0 bytes. Strings are identified
42 // by an offset in such region.
43 // The `StringsTable` points to this region in the parsed ObjectFile.
44 StringRef StringsTable;
45
46 // A copy of types table from the object file but using native byte
47 // order. Should not be too big in practice, e.g. for ~250MiB vmlinux
48 // image it is ~4MiB.
49 OwningArrayRef<uint8_t> TypesBuffer;
50
51 // Maps ELF section number to instruction line number information.
52 // Each BTFLinesVector is sorted by `InsnOffset` to allow fast lookups.
54
55 // Maps ELF section number to CO-RE relocation information.
56 // Each BTFRelocVector is sorted by `InsnOffset` to allow fast lookups.
58
59 // Vector of pointers to all known types, index in this vector
60 // equals to logical type BTF id.
61 // Pointers point to memory owned by `TypesBuffer`
62 // (except pointer at index 0, which is statically allocated).
63 std::vector<const BTF::CommonType *> Types;
64
65 struct ParseContext;
66 Error parseBTF(ParseContext &Ctx, SectionRef BTF);
67 Error parseBTFExt(ParseContext &Ctx, SectionRef BTFExt);
68 Error parseLineInfo(ParseContext &Ctx, DataExtractor &Extractor,
69 uint64_t LineInfoStart, uint64_t LineInfoEnd);
70 Error parseRelocInfo(ParseContext &Ctx, DataExtractor &Extractor,
71 uint64_t RelocInfoStart, uint64_t RelocInfoEnd);
72 Error parseTypesInfo(ParseContext &Ctx, uint64_t TypesInfoStart,
73 StringRef RawData);
74
75public:
76 // Looks-up a string in the .BTF section's string table.
77 // Offset is relative to string table start.
79
80 // Search for line information for a specific address,
81 // address match is exact (contrary to DWARFContext).
82 // Return nullptr if no information found.
83 // If information is present, return a pointer to object
84 // owned by this class.
86
87 // Search for CO-RE relocation information for a specific address.
88 // Return nullptr if no information found.
89 // If information is present, return a pointer to object
90 // owned by this class.
93
94 // Return a human readable representation of the CO-RE relocation
95 // record, this is for display purpose only.
96 // See implementation for details.
97 LLVM_ABI void symbolize(const BTF::BPFFieldReloc *Reloc,
98 SmallVectorImpl<char> &Result) const;
99
100 // Lookup BTF type definition with a specific index.
101 // Return nullptr if no information found.
102 // If information is present, return a pointer to object
103 // owned by this class.
104 LLVM_ABI const BTF::CommonType *findType(uint32_t Id) const;
105
106 // Return total number of known BTF types.
107 size_t typesCount() const { return Types.size(); }
108
109 // Allow to selectively load BTF information.
111 bool LoadLines = false;
112 bool LoadTypes = false;
113 bool LoadRelocs = false;
114 };
115
116 // Fills instance of BTFParser with information stored in .BTF and
117 // .BTF.ext sections of the `Obj`. If this instance was already
118 // filled, old data is discarded.
119 //
120 // If information cannot be parsed:
121 // - return an error describing the failure;
122 // - state of the BTFParser might be incomplete but is not invalid,
123 // queries might be run against it, but some (or all) information
124 // might be unavailable;
125 LLVM_ABI Error parse(const ObjectFile &Obj, const ParseOptions &Opts);
126 Error parse(const ObjectFile &Obj) { return parse(Obj, {true, true, true}); }
127
128 // Return true if `Obj` has .BTF and .BTF.ext sections.
129 LLVM_ABI static bool hasBTFSections(const ObjectFile &Obj);
130};
131
132} // namespace llvm
133
134#endif // LLVM_DEBUGINFO_BTF_BTFPARSER_H
This file contains the layout of .BTF and .BTF.ext ELF sections.
#define LLVM_ABI
Definition: Compiler.h:213
This file defines the DenseMap class.
size_t typesCount() const
Definition: BTFParser.h:107
LLVM_ABI StringRef findString(uint32_t Offset) const
Definition: BTFParser.cpp:427
Error parse(const ObjectFile &Obj)
Definition: BTFParser.h:126
LLVM_ABI const BTF::BPFLineInfo * findLineInfo(SectionedAddress Address) const
Definition: BTFParser.cpp:449
LLVM_ABI void symbolize(const BTF::BPFFieldReloc *Reloc, SmallVectorImpl< char > &Result) const
Definition: BTFParser.cpp:657
LLVM_ABI const BTF::CommonType * findType(uint32_t Id) const
Definition: BTFParser.cpp:458
static LLVM_ABI bool hasBTFSections(const ObjectFile &Obj)
Definition: BTFParser.cpp:410
LLVM_ABI const BTF::BPFFieldReloc * findFieldReloc(SectionedAddress Address) const
Definition: BTFParser.cpp:454
Lightweight error class with error context and mandatory checking.
Definition: Error.h:159
This is a MutableArrayRef that owns its array.
Definition: ArrayRef.h:454
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:574
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1197
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:55
This class is the base class for all object file types.
Definition: ObjectFile.h:231
This is a value type class that represents a single section in the list of sections in the object fil...
Definition: ObjectFile.h:83
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:477
Specifying one offset relocation.
Definition: BTF.h:267
Specifying one line info.
Definition: BTF.h:250
The BTF common type definition.
Definition: BTF.h:107
Definition: regcomp.c:186