95 lines · cpp
1//===- DebugCrossImpSubsection.cpp ----------------------------------------===//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-exception6//7//===----------------------------------------------------------------------===//8 9#include "llvm/DebugInfo/CodeView/DebugCrossImpSubsection.h"10#include "llvm/ADT/ArrayRef.h"11#include "llvm/DebugInfo/CodeView/CodeViewError.h"12#include "llvm/DebugInfo/CodeView/DebugStringTableSubsection.h"13#include "llvm/Support/BinaryStreamReader.h"14#include "llvm/Support/BinaryStreamWriter.h"15#include "llvm/Support/Endian.h"16#include "llvm/Support/Error.h"17#include <cstdint>18#include <vector>19 20using namespace llvm;21using namespace llvm::codeview;22 23Error VarStreamArrayExtractor<CrossModuleImportItem>::24operator()(BinaryStreamRef Stream, uint32_t &Len,25 codeview::CrossModuleImportItem &Item) {26 BinaryStreamReader Reader(Stream);27 if (Reader.bytesRemaining() < sizeof(CrossModuleImport))28 return make_error<CodeViewError>(29 cv_error_code::insufficient_buffer,30 "Not enough bytes for a Cross Module Import Header!");31 if (auto EC = Reader.readObject(Item.Header))32 return EC;33 if (Reader.bytesRemaining() < Item.Header->Count * sizeof(uint32_t))34 return make_error<CodeViewError>(35 cv_error_code::insufficient_buffer,36 "Not enough to read specified number of Cross Module References!");37 if (auto EC = Reader.readArray(Item.Imports, Item.Header->Count))38 return EC;39 return Error::success();40}41 42Error DebugCrossModuleImportsSubsectionRef::initialize(43 BinaryStreamReader Reader) {44 return Reader.readArray(References, Reader.bytesRemaining());45}46 47Error DebugCrossModuleImportsSubsectionRef::initialize(BinaryStreamRef Stream) {48 BinaryStreamReader Reader(Stream);49 return initialize(Reader);50}51 52void DebugCrossModuleImportsSubsection::addImport(StringRef Module,53 uint32_t ImportId) {54 Strings.insert(Module);55 std::vector<support::ulittle32_t> Targets = {support::ulittle32_t(ImportId)};56 auto Result = Mappings.insert(std::make_pair(Module, Targets));57 if (!Result.second)58 Result.first->getValue().push_back(Targets[0]);59}60 61uint32_t DebugCrossModuleImportsSubsection::calculateSerializedSize() const {62 uint32_t Size = 0;63 for (const auto &Item : Mappings) {64 Size += sizeof(CrossModuleImport);65 Size += sizeof(support::ulittle32_t) * Item.second.size();66 }67 return Size;68}69 70Error DebugCrossModuleImportsSubsection::commit(71 BinaryStreamWriter &Writer) const {72 using T = decltype(&*Mappings.begin());73 std::vector<T> Ids;74 Ids.reserve(Mappings.size());75 76 for (const auto &M : Mappings)77 Ids.push_back(&M);78 79 llvm::sort(Ids, [this](const T &L1, const T &L2) {80 return Strings.getIdForString(L1->getKey()) <81 Strings.getIdForString(L2->getKey());82 });83 84 for (const auto &Item : Ids) {85 CrossModuleImport Imp;86 Imp.ModuleNameOffset = Strings.getIdForString(Item->getKey());87 Imp.Count = Item->getValue().size();88 if (auto EC = Writer.writeObject(Imp))89 return EC;90 if (auto EC = Writer.writeArray(ArrayRef(Item->getValue())))91 return EC;92 }93 return Error::success();94}95