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