Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:43:39

0001 //===- BTFParser.h ----------------------------------------------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 // BTFParser reads .BTF and .BTF.ext ELF sections generated by LLVM
0010 // BPF backend and provides introspection for the stored information.
0011 // Currently the following information is accessible:
0012 // - string table;
0013 // - instruction offset to line information mapping;
0014 // - types table;
0015 // - CO-RE relocations table.
0016 //
0017 // See llvm/DebugInfo/BTF/BTF.h for some details about binary format
0018 // and links to Linux Kernel documentation.
0019 //
0020 //===----------------------------------------------------------------------===//
0021 
0022 #ifndef LLVM_DEBUGINFO_BTF_BTFPARSER_H
0023 #define LLVM_DEBUGINFO_BTF_BTFPARSER_H
0024 
0025 #include "llvm/ADT/DenseMap.h"
0026 #include "llvm/DebugInfo/BTF/BTF.h"
0027 #include "llvm/Object/ObjectFile.h"
0028 #include "llvm/Support/DataExtractor.h"
0029 
0030 namespace llvm {
0031 using object::ObjectFile;
0032 using object::SectionedAddress;
0033 using object::SectionRef;
0034 
0035 class BTFParser {
0036   using BTFLinesVector = SmallVector<BTF::BPFLineInfo, 0>;
0037   using BTFRelocVector = SmallVector<BTF::BPFFieldReloc, 0>;
0038 
0039   // In BTF strings are stored as a continuous memory region with
0040   // individual strings separated by 0 bytes. Strings are identified
0041   // by an offset in such region.
0042   // The `StringsTable` points to this region in the parsed ObjectFile.
0043   StringRef StringsTable;
0044 
0045   // A copy of types table from the object file but using native byte
0046   // order. Should not be too big in practice, e.g. for ~250MiB vmlinux
0047   // image it is ~4MiB.
0048   OwningArrayRef<uint8_t> TypesBuffer;
0049 
0050   // Maps ELF section number to instruction line number information.
0051   // Each BTFLinesVector is sorted by `InsnOffset` to allow fast lookups.
0052   DenseMap<uint64_t, BTFLinesVector> SectionLines;
0053 
0054   // Maps ELF section number to CO-RE relocation information.
0055   // Each BTFRelocVector is sorted by `InsnOffset` to allow fast lookups.
0056   DenseMap<uint64_t, BTFRelocVector> SectionRelocs;
0057 
0058   // Vector of pointers to all known types, index in this vector
0059   // equals to logical type BTF id.
0060   // Pointers point to memory owned by `TypesBuffer`
0061   // (except pointer at index 0, which is statically allocated).
0062   std::vector<const BTF::CommonType *> Types;
0063 
0064   struct ParseContext;
0065   Error parseBTF(ParseContext &Ctx, SectionRef BTF);
0066   Error parseBTFExt(ParseContext &Ctx, SectionRef BTFExt);
0067   Error parseLineInfo(ParseContext &Ctx, DataExtractor &Extractor,
0068                       uint64_t LineInfoStart, uint64_t LineInfoEnd);
0069   Error parseRelocInfo(ParseContext &Ctx, DataExtractor &Extractor,
0070                        uint64_t RelocInfoStart, uint64_t RelocInfoEnd);
0071   Error parseTypesInfo(ParseContext &Ctx, uint64_t TypesInfoStart,
0072                        StringRef RawData);
0073 
0074 public:
0075   // Looks-up a string in the .BTF section's string table.
0076   // Offset is relative to string table start.
0077   StringRef findString(uint32_t Offset) const;
0078 
0079   // Search for line information for a specific address,
0080   // address match is exact (contrary to DWARFContext).
0081   // Return nullptr if no information found.
0082   // If information is present, return a pointer to object
0083   // owned by this class.
0084   const BTF::BPFLineInfo *findLineInfo(SectionedAddress Address) const;
0085 
0086   // Search for CO-RE relocation information for a specific address.
0087   // Return nullptr if no information found.
0088   // If information is present, return a pointer to object
0089   // owned by this class.
0090   const BTF::BPFFieldReloc *findFieldReloc(SectionedAddress Address) const;
0091 
0092   // Return a human readable representation of the CO-RE relocation
0093   // record, this is for display purpose only.
0094   // See implementation for details.
0095   void symbolize(const BTF::BPFFieldReloc *Reloc,
0096                  SmallVectorImpl<char> &Result) const;
0097 
0098   // Lookup BTF type definition with a specific index.
0099   // Return nullptr if no information found.
0100   // If information is present, return a pointer to object
0101   // owned by this class.
0102   const BTF::CommonType *findType(uint32_t Id) const;
0103 
0104   // Return total number of known BTF types.
0105   size_t typesCount() const { return Types.size(); }
0106 
0107   // Allow to selectively load BTF information.
0108   struct ParseOptions {
0109     bool LoadLines = false;
0110     bool LoadTypes = false;
0111     bool LoadRelocs = false;
0112   };
0113 
0114   // Fills instance of BTFParser with information stored in .BTF and
0115   // .BTF.ext sections of the `Obj`. If this instance was already
0116   // filled, old data is discarded.
0117   //
0118   // If information cannot be parsed:
0119   // - return an error describing the failure;
0120   // - state of the BTFParser might be incomplete but is not invalid,
0121   //   queries might be run against it, but some (or all) information
0122   //   might be unavailable;
0123   Error parse(const ObjectFile &Obj, const ParseOptions &Opts);
0124   Error parse(const ObjectFile &Obj) { return parse(Obj, {true, true, true}); }
0125 
0126   // Return true if `Obj` has .BTF and .BTF.ext sections.
0127   static bool hasBTFSections(const ObjectFile &Obj);
0128 };
0129 
0130 } // namespace llvm
0131 
0132 #endif // LLVM_DEBUGINFO_BTF_BTFPARSER_H