Path: blob/main/contrib/llvm-project/llvm/lib/DebugInfo/GSYM/MergedFunctionsInfo.cpp
213799 views
//===- MergedFunctionsInfo.cpp ----------------------------------*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "llvm/DebugInfo/GSYM/MergedFunctionsInfo.h"9#include "llvm/DebugInfo/GSYM/FileWriter.h"10#include "llvm/DebugInfo/GSYM/FunctionInfo.h"11#include "llvm/Support/DataExtractor.h"1213using namespace llvm;14using namespace gsym;1516void MergedFunctionsInfo::clear() { MergedFunctions.clear(); }1718llvm::Error MergedFunctionsInfo::encode(FileWriter &Out) const {19Out.writeU32(MergedFunctions.size());20for (const auto &F : MergedFunctions) {21Out.writeU32(0);22const auto StartOffset = Out.tell();23// Encode the FunctionInfo with no padding so later we can just read them24// one after the other without knowing the offset in the stream for each.25llvm::Expected<uint64_t> result = F.encode(Out, /*NoPadding =*/true);26if (!result)27return result.takeError();28const auto Length = Out.tell() - StartOffset;29Out.fixup32(static_cast<uint32_t>(Length), StartOffset - 4);30}31return Error::success();32}3334llvm::Expected<MergedFunctionsInfo>35MergedFunctionsInfo::decode(DataExtractor &Data, uint64_t BaseAddr) {36MergedFunctionsInfo MFI;37auto FuncExtractorsOrError = MFI.getFuncsDataExtractors(Data);3839if (!FuncExtractorsOrError)40return FuncExtractorsOrError.takeError();4142for (DataExtractor &FuncData : *FuncExtractorsOrError) {43llvm::Expected<FunctionInfo> FI = FunctionInfo::decode(FuncData, BaseAddr);44if (!FI)45return FI.takeError();46MFI.MergedFunctions.push_back(std::move(*FI));47}4849return MFI;50}5152llvm::Expected<std::vector<DataExtractor>>53MergedFunctionsInfo::getFuncsDataExtractors(DataExtractor &Data) {54std::vector<DataExtractor> Results;55uint64_t Offset = 0;5657// Ensure there is enough data to read the function count.58if (!Data.isValidOffsetForDataOfSize(Offset, 4))59return createStringError(60std::errc::io_error,61"unable to read the function count at offset 0x%8.8" PRIx64, Offset);6263uint32_t Count = Data.getU32(&Offset);6465for (uint32_t i = 0; i < Count; ++i) {66// Ensure there is enough data to read the function size.67if (!Data.isValidOffsetForDataOfSize(Offset, 4))68return createStringError(69std::errc::io_error,70"unable to read size of function %u at offset 0x%8.8" PRIx64, i,71Offset);7273uint32_t FnSize = Data.getU32(&Offset);7475// Ensure there is enough data for the function content.76if (!Data.isValidOffsetForDataOfSize(Offset, FnSize))77return createStringError(78std::errc::io_error,79"function data is truncated for function %u at offset 0x%8.8" PRIx6480", expected size %u",81i, Offset, FnSize);8283// Extract the function data.84Results.emplace_back(Data.getData().substr(Offset, FnSize),85Data.isLittleEndian(), Data.getAddressSize());8687Offset += FnSize;88}89return Results;90}9192bool operator==(const MergedFunctionsInfo &LHS,93const MergedFunctionsInfo &RHS) {94return LHS.MergedFunctions == RHS.MergedFunctions;95}969798