xref: /freebsd/contrib/llvm-project/llvm/include/llvm/DebugInfo/BTF/BTFParser.h (revision 700637cbb5e582861067a11aaca4d053546871d2)
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"
26 #include "llvm/DebugInfo/BTF/BTF.h"
27 #include "llvm/Object/ObjectFile.h"
28 #include "llvm/Support/Compiler.h"
29 #include "llvm/Support/DataExtractor.h"
30 
31 namespace llvm {
32 using object::ObjectFile;
33 using object::SectionedAddress;
34 using object::SectionRef;
35 
36 class BTFParser {
37   using BTFLinesVector = SmallVector<BTF::BPFLineInfo, 0>;
38   using BTFRelocVector = SmallVector<BTF::BPFFieldReloc, 0>;
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.
53   DenseMap<uint64_t, BTFLinesVector> SectionLines;
54 
55   // Maps ELF section number to CO-RE relocation information.
56   // Each BTFRelocVector is sorted by `InsnOffset` to allow fast lookups.
57   DenseMap<uint64_t, BTFRelocVector> SectionRelocs;
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 
75 public:
76   // Looks-up a string in the .BTF section's string table.
77   // Offset is relative to string table start.
78   LLVM_ABI StringRef findString(uint32_t Offset) const;
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.
85   LLVM_ABI const BTF::BPFLineInfo *findLineInfo(SectionedAddress Address) const;
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.
91   LLVM_ABI const BTF::BPFFieldReloc *
92   findFieldReloc(SectionedAddress Address) const;
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.
typesCount()107   size_t typesCount() const { return Types.size(); }
108 
109   // Allow to selectively load BTF information.
110   struct ParseOptions {
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);
parse(const ObjectFile & Obj)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
135