LLVM 20.0.0git
DebugCrossImpSubsection.cpp
Go to the documentation of this file.
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-exception
6//
7//===----------------------------------------------------------------------===//
8
10#include "llvm/ADT/ArrayRef.h"
15#include "llvm/Support/Endian.h"
16#include "llvm/Support/Error.h"
17#include <cstdint>
18#include <utility>
19#include <vector>
20
21using namespace llvm;
22using namespace llvm::codeview;
23
27 BinaryStreamReader Reader(Stream);
28 if (Reader.bytesRemaining() < sizeof(CrossModuleImport))
29 return make_error<CodeViewError>(
30 cv_error_code::insufficient_buffer,
31 "Not enough bytes for a Cross Module Import Header!");
32 if (auto EC = Reader.readObject(Item.Header))
33 return EC;
34 if (Reader.bytesRemaining() < Item.Header->Count * sizeof(uint32_t))
35 return make_error<CodeViewError>(
36 cv_error_code::insufficient_buffer,
37 "Not enough to read specified number of Cross Module References!");
38 if (auto EC = Reader.readArray(Item.Imports, Item.Header->Count))
39 return EC;
40 return Error::success();
41}
42
44 BinaryStreamReader Reader) {
45 return Reader.readArray(References, Reader.bytesRemaining());
46}
47
49 BinaryStreamReader Reader(Stream);
50 return initialize(Reader);
51}
52
54 uint32_t ImportId) {
55 Strings.insert(Module);
56 std::vector<support::ulittle32_t> Targets = {support::ulittle32_t(ImportId)};
57 auto Result = Mappings.insert(std::make_pair(Module, Targets));
58 if (!Result.second)
59 Result.first->getValue().push_back(Targets[0]);
60}
61
63 uint32_t Size = 0;
64 for (const auto &Item : Mappings) {
65 Size += sizeof(CrossModuleImport);
66 Size += sizeof(support::ulittle32_t) * Item.second.size();
67 }
68 return Size;
69}
70
72 BinaryStreamWriter &Writer) const {
73 using T = decltype(&*Mappings.begin());
74 std::vector<T> Ids;
75 Ids.reserve(Mappings.size());
76
77 for (const auto &M : Mappings)
78 Ids.push_back(&M);
79
80 llvm::sort(Ids, [this](const T &L1, const T &L2) {
81 return Strings.getIdForString(L1->getKey()) <
82 Strings.getIdForString(L2->getKey());
83 });
84
85 for (const auto &Item : Ids) {
87 Imp.ModuleNameOffset = Strings.getIdForString(Item->getKey());
88 Imp.Count = Item->getValue().size();
89 if (auto EC = Writer.writeObject(Imp))
90 return EC;
91 if (auto EC = Writer.writeArray(ArrayRef(Item->getValue())))
92 return EC;
93 }
94 return Error::success();
95}
uint64_t Size
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
Provides read only access to a subclass of BinaryStream.
uint64_t bytesRemaining() const
Error readArray(ArrayRef< T > &Array, uint32_t NumElements)
Get a reference to a NumElements element array of objects of type T from the underlying stream as if ...
BinaryStreamRef is to BinaryStream what ArrayRef is to an Array.
Provides write only access to a subclass of WritableBinaryStream.
Error writeArray(ArrayRef< T > Array)
Writes an array of objects of type T to the underlying stream, as if by using memcpy.
Error writeObject(const T &Obj)
Writes the object Obj to the underlying stream, as if by using memcpy.
Lightweight error class with error context and mandatory checking.
Definition: Error.h:160
static ErrorSuccess success()
Create a success value.
Definition: Error.h:337
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
void addImport(StringRef Module, uint32_t ImportId)
Error commit(BinaryStreamWriter &Writer) const override
detail::packed_endian_specific_integral< uint32_t, llvm::endianness::little, unaligned > ulittle32_t
Definition: Endian.h:285
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
void sort(IteratorTy Start, IteratorTy End)
Definition: STLExtras.h:1664
Error operator()(BinaryStreamRef Stream, uint32_t &Len, T &Item) const =delete
FixedStreamArray< support::ulittle32_t > Imports
support::ulittle32_t Count
Definition: CodeView.h:615
support::ulittle32_t ModuleNameOffset
Definition: CodeView.h:614