1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
|
//===- BTFParser.cpp ------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// BTFParser reads/interprets .BTF and .BTF.ext ELF sections.
// Refer to BTFParser.h for API description.
//
//===----------------------------------------------------------------------===//
#include "llvm/DebugInfo/BTF/BTFParser.h"
#include "llvm/Support/Errc.h"
#define DEBUG_TYPE "debug-info-btf-parser"
using namespace llvm;
using object::ObjectFile;
using object::SectionedAddress;
using object::SectionRef;
const char BTFSectionName[] = ".BTF";
const char BTFExtSectionName[] = ".BTF.ext";
// Utility class with API similar to raw_ostream but can be cast
// to Error, e.g.:
//
// Error foo(...) {
// ...
// if (Error E = bar(...))
// return Err("error while foo(): ") << E;
// ...
// }
//
namespace {
class Err {
std::string Buffer;
raw_string_ostream Stream;
public:
Err(const char *InitialMsg) : Buffer(InitialMsg), Stream(Buffer) {}
Err(const char *SectionName, DataExtractor::Cursor &C)
: Buffer(), Stream(Buffer) {
*this << "error while reading " << SectionName
<< " section: " << C.takeError();
};
template <typename T> Err &operator<<(T Val) {
Stream << Val;
return *this;
}
Err &write_hex(unsigned long long Val) {
Stream.write_hex(Val);
return *this;
}
Err &operator<<(Error Val) {
handleAllErrors(std::move(Val),
[=](ErrorInfoBase &Info) { Stream << Info.message(); });
return *this;
}
operator Error() const {
return make_error<StringError>(Buffer, errc::invalid_argument);
}
};
} // anonymous namespace
// ParseContext wraps information that is only necessary while parsing
// ObjectFile and can be discarded once parsing is done.
// Used by BTFParser::parse* auxiliary functions.
struct BTFParser::ParseContext {
const ObjectFile &Obj;
// Map from ELF section name to SectionRef
DenseMap<StringRef, SectionRef> Sections;
public:
ParseContext(const ObjectFile &Obj) : Obj(Obj) {}
Expected<DataExtractor> makeExtractor(SectionRef Sec) {
Expected<StringRef> Contents = Sec.getContents();
if (!Contents)
return Contents.takeError();
return DataExtractor(Contents.get(), Obj.isLittleEndian(),
Obj.getBytesInAddress());
}
std::optional<SectionRef> findSection(StringRef Name) const {
auto It = Sections.find(Name);
if (It != Sections.end())
return It->second;
return std::nullopt;
}
};
Error BTFParser::parseBTF(ParseContext &Ctx, SectionRef BTF) {
Expected<DataExtractor> MaybeExtractor = Ctx.makeExtractor(BTF);
if (!MaybeExtractor)
return MaybeExtractor.takeError();
DataExtractor &Extractor = MaybeExtractor.get();
DataExtractor::Cursor C = DataExtractor::Cursor(0);
uint16_t Magic = Extractor.getU16(C);
if (!C)
return Err(".BTF", C);
if (Magic != BTF::MAGIC)
return Err("invalid .BTF magic: ").write_hex(Magic);
uint8_t Version = Extractor.getU8(C);
if (!C)
return Err(".BTF", C);
if (Version != 1)
return Err("unsupported .BTF version: ") << (unsigned)Version;
(void)Extractor.getU8(C); // flags
uint32_t HdrLen = Extractor.getU32(C);
if (!C)
return Err(".BTF", C);
if (HdrLen < 8)
return Err("unexpected .BTF header length: ") << HdrLen;
(void)Extractor.getU32(C); // type_off
(void)Extractor.getU32(C); // type_len
uint32_t StrOff = Extractor.getU32(C);
uint32_t StrLen = Extractor.getU32(C);
uint32_t StrStart = HdrLen + StrOff;
uint32_t StrEnd = StrStart + StrLen;
if (!C)
return Err(".BTF", C);
if (Extractor.getData().size() < StrEnd)
return Err("invalid .BTF section size, expecting at-least ")
<< StrEnd << " bytes";
StringsTable = Extractor.getData().substr(StrStart, StrLen);
return Error::success();
}
Error BTFParser::parseBTFExt(ParseContext &Ctx, SectionRef BTFExt) {
Expected<DataExtractor> MaybeExtractor = Ctx.makeExtractor(BTFExt);
if (!MaybeExtractor)
return MaybeExtractor.takeError();
DataExtractor &Extractor = MaybeExtractor.get();
DataExtractor::Cursor C = DataExtractor::Cursor(0);
uint16_t Magic = Extractor.getU16(C);
if (!C)
return Err(".BTF.ext", C);
if (Magic != BTF::MAGIC)
return Err("invalid .BTF.ext magic: ").write_hex(Magic);
uint8_t Version = Extractor.getU8(C);
if (!C)
return Err(".BTF", C);
if (Version != 1)
return Err("unsupported .BTF.ext version: ") << (unsigned)Version;
(void)Extractor.getU8(C); // flags
uint32_t HdrLen = Extractor.getU32(C);
if (!C)
return Err(".BTF.ext", C);
if (HdrLen < 8)
return Err("unexpected .BTF.ext header length: ") << HdrLen;
(void)Extractor.getU32(C); // func_info_off
(void)Extractor.getU32(C); // func_info_len
uint32_t LineInfoOff = Extractor.getU32(C);
uint32_t LineInfoLen = Extractor.getU32(C);
if (!C)
return Err(".BTF.ext", C);
uint32_t LineInfoStart = HdrLen + LineInfoOff;
uint32_t LineInfoEnd = LineInfoStart + LineInfoLen;
if (Error E = parseLineInfo(Ctx, Extractor, LineInfoStart, LineInfoEnd))
return E;
return Error::success();
}
Error BTFParser::parseLineInfo(ParseContext &Ctx, DataExtractor &Extractor,
uint64_t LineInfoStart, uint64_t LineInfoEnd) {
DataExtractor::Cursor C = DataExtractor::Cursor(LineInfoStart);
uint32_t RecSize = Extractor.getU32(C);
if (!C)
return Err(".BTF.ext", C);
if (RecSize < 16)
return Err("unexpected .BTF.ext line info record length: ") << RecSize;
while (C && C.tell() < LineInfoEnd) {
uint32_t SecNameOff = Extractor.getU32(C);
uint32_t NumInfo = Extractor.getU32(C);
StringRef SecName = findString(SecNameOff);
std::optional<SectionRef> Sec = Ctx.findSection(SecName);
if (!C)
return Err(".BTF.ext", C);
if (!Sec)
return Err("") << "can't find section '" << SecName
<< "' while parsing .BTF.ext line info";
BTFLinesVector &Lines = SectionLines[Sec->getIndex()];
for (uint32_t I = 0; C && I < NumInfo; ++I) {
uint64_t RecStart = C.tell();
uint32_t InsnOff = Extractor.getU32(C);
uint32_t FileNameOff = Extractor.getU32(C);
uint32_t LineOff = Extractor.getU32(C);
uint32_t LineCol = Extractor.getU32(C);
if (!C)
return Err(".BTF.ext", C);
Lines.push_back({InsnOff, FileNameOff, LineOff, LineCol});
C.seek(RecStart + RecSize);
}
llvm::stable_sort(Lines,
[](const BTF::BPFLineInfo &L, const BTF::BPFLineInfo &R) {
return L.InsnOffset < R.InsnOffset;
});
}
if (!C)
return Err(".BTF.ext", C);
return Error::success();
}
Error BTFParser::parse(const ObjectFile &Obj) {
StringsTable = StringRef();
SectionLines.clear();
ParseContext Ctx(Obj);
std::optional<SectionRef> BTF;
std::optional<SectionRef> BTFExt;
for (SectionRef Sec : Obj.sections()) {
Expected<StringRef> MaybeName = Sec.getName();
if (!MaybeName)
return Err("error while reading section name: ") << MaybeName.takeError();
Ctx.Sections[*MaybeName] = Sec;
if (*MaybeName == BTFSectionName)
BTF = Sec;
if (*MaybeName == BTFExtSectionName)
BTFExt = Sec;
}
if (!BTF)
return Err("can't find .BTF section");
if (!BTFExt)
return Err("can't find .BTF.ext section");
if (Error E = parseBTF(Ctx, *BTF))
return E;
if (Error E = parseBTFExt(Ctx, *BTFExt))
return E;
return Error::success();
}
bool BTFParser::hasBTFSections(const ObjectFile &Obj) {
bool HasBTF = false;
bool HasBTFExt = false;
for (SectionRef Sec : Obj.sections()) {
Expected<StringRef> Name = Sec.getName();
if (Error E = Name.takeError()) {
logAllUnhandledErrors(std::move(E), errs());
continue;
}
HasBTF |= *Name == BTFSectionName;
HasBTFExt |= *Name == BTFExtSectionName;
if (HasBTF && HasBTFExt)
return true;
}
return false;
}
StringRef BTFParser::findString(uint32_t Offset) const {
return StringsTable.slice(Offset, StringsTable.find(0, Offset));
}
const BTF::BPFLineInfo *
BTFParser::findLineInfo(SectionedAddress Address) const {
auto MaybeSecInfo = SectionLines.find(Address.SectionIndex);
if (MaybeSecInfo == SectionLines.end())
return nullptr;
const BTFLinesVector &SecInfo = MaybeSecInfo->second;
const uint64_t TargetOffset = Address.Address;
BTFLinesVector::const_iterator LineInfo =
llvm::partition_point(SecInfo, [=](const BTF::BPFLineInfo &Line) {
return Line.InsnOffset < TargetOffset;
});
if (LineInfo == SecInfo.end() || LineInfo->InsnOffset != Address.Address)
return nullptr;
return LineInfo;
}
|