xref: /src/contrib/llvm-project/llvm/lib/DebugInfo/CodeView/DebugCrossImpSubsection.cpp (revision bdd1243df58e60e85101c09001d9812a789b6bc4)
19df3605dSDimitry Andric //===- DebugCrossImpSubsection.cpp ----------------------------------------===//
27ab83427SDimitry Andric //
3e6d15924SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e6d15924SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5e6d15924SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
67ab83427SDimitry Andric //
77ab83427SDimitry Andric //===----------------------------------------------------------------------===//
87ab83427SDimitry Andric 
97ab83427SDimitry Andric #include "llvm/DebugInfo/CodeView/DebugCrossImpSubsection.h"
109df3605dSDimitry Andric #include "llvm/ADT/ArrayRef.h"
117ab83427SDimitry Andric #include "llvm/DebugInfo/CodeView/CodeViewError.h"
127ab83427SDimitry Andric #include "llvm/DebugInfo/CodeView/DebugStringTableSubsection.h"
139df3605dSDimitry Andric #include "llvm/Support/BinaryStreamReader.h"
149df3605dSDimitry Andric #include "llvm/Support/BinaryStreamWriter.h"
159df3605dSDimitry Andric #include "llvm/Support/Endian.h"
169df3605dSDimitry Andric #include "llvm/Support/Error.h"
179df3605dSDimitry Andric #include <algorithm>
189df3605dSDimitry Andric #include <cstdint>
199df3605dSDimitry Andric #include <utility>
209df3605dSDimitry Andric #include <vector>
217ab83427SDimitry Andric 
227ab83427SDimitry Andric using namespace llvm;
237ab83427SDimitry Andric using namespace llvm::codeview;
247ab83427SDimitry Andric 
257ab83427SDimitry Andric Error VarStreamArrayExtractor<CrossModuleImportItem>::
operator ()(BinaryStreamRef Stream,uint32_t & Len,codeview::CrossModuleImportItem & Item)267ab83427SDimitry Andric operator()(BinaryStreamRef Stream, uint32_t &Len,
277ab83427SDimitry Andric            codeview::CrossModuleImportItem &Item) {
287ab83427SDimitry Andric   BinaryStreamReader Reader(Stream);
297ab83427SDimitry Andric   if (Reader.bytesRemaining() < sizeof(CrossModuleImport))
307ab83427SDimitry Andric     return make_error<CodeViewError>(
317ab83427SDimitry Andric         cv_error_code::insufficient_buffer,
327ab83427SDimitry Andric         "Not enough bytes for a Cross Module Import Header!");
337ab83427SDimitry Andric   if (auto EC = Reader.readObject(Item.Header))
347ab83427SDimitry Andric     return EC;
357ab83427SDimitry Andric   if (Reader.bytesRemaining() < Item.Header->Count * sizeof(uint32_t))
367ab83427SDimitry Andric     return make_error<CodeViewError>(
377ab83427SDimitry Andric         cv_error_code::insufficient_buffer,
387ab83427SDimitry Andric         "Not enough to read specified number of Cross Module References!");
397ab83427SDimitry Andric   if (auto EC = Reader.readArray(Item.Imports, Item.Header->Count))
407ab83427SDimitry Andric     return EC;
417ab83427SDimitry Andric   return Error::success();
427ab83427SDimitry Andric }
437ab83427SDimitry Andric 
initialize(BinaryStreamReader Reader)447ab83427SDimitry Andric Error DebugCrossModuleImportsSubsectionRef::initialize(
457ab83427SDimitry Andric     BinaryStreamReader Reader) {
467ab83427SDimitry Andric   return Reader.readArray(References, Reader.bytesRemaining());
477ab83427SDimitry Andric }
487ab83427SDimitry Andric 
initialize(BinaryStreamRef Stream)497ab83427SDimitry Andric Error DebugCrossModuleImportsSubsectionRef::initialize(BinaryStreamRef Stream) {
507ab83427SDimitry Andric   BinaryStreamReader Reader(Stream);
517ab83427SDimitry Andric   return initialize(Reader);
527ab83427SDimitry Andric }
537ab83427SDimitry Andric 
addImport(StringRef Module,uint32_t ImportId)547ab83427SDimitry Andric void DebugCrossModuleImportsSubsection::addImport(StringRef Module,
557ab83427SDimitry Andric                                                   uint32_t ImportId) {
567ab83427SDimitry Andric   Strings.insert(Module);
577ab83427SDimitry Andric   std::vector<support::ulittle32_t> Targets = {support::ulittle32_t(ImportId)};
587ab83427SDimitry Andric   auto Result = Mappings.insert(std::make_pair(Module, Targets));
597ab83427SDimitry Andric   if (!Result.second)
607ab83427SDimitry Andric     Result.first->getValue().push_back(Targets[0]);
617ab83427SDimitry Andric }
627ab83427SDimitry Andric 
calculateSerializedSize() const637ab83427SDimitry Andric uint32_t DebugCrossModuleImportsSubsection::calculateSerializedSize() const {
647ab83427SDimitry Andric   uint32_t Size = 0;
657ab83427SDimitry Andric   for (const auto &Item : Mappings) {
667ab83427SDimitry Andric     Size += sizeof(CrossModuleImport);
677ab83427SDimitry Andric     Size += sizeof(support::ulittle32_t) * Item.second.size();
687ab83427SDimitry Andric   }
697ab83427SDimitry Andric   return Size;
707ab83427SDimitry Andric }
717ab83427SDimitry Andric 
commit(BinaryStreamWriter & Writer) const727ab83427SDimitry Andric Error DebugCrossModuleImportsSubsection::commit(
737ab83427SDimitry Andric     BinaryStreamWriter &Writer) const {
747ab83427SDimitry Andric   using T = decltype(&*Mappings.begin());
757ab83427SDimitry Andric   std::vector<T> Ids;
767ab83427SDimitry Andric   Ids.reserve(Mappings.size());
777ab83427SDimitry Andric 
787ab83427SDimitry Andric   for (const auto &M : Mappings)
797ab83427SDimitry Andric     Ids.push_back(&M);
807ab83427SDimitry Andric 
81d8e91e46SDimitry Andric   llvm::sort(Ids, [this](const T &L1, const T &L2) {
82eb11fae6SDimitry Andric     return Strings.getIdForString(L1->getKey()) <
83eb11fae6SDimitry Andric            Strings.getIdForString(L2->getKey());
847ab83427SDimitry Andric   });
857ab83427SDimitry Andric 
867ab83427SDimitry Andric   for (const auto &Item : Ids) {
877ab83427SDimitry Andric     CrossModuleImport Imp;
88eb11fae6SDimitry Andric     Imp.ModuleNameOffset = Strings.getIdForString(Item->getKey());
897ab83427SDimitry Andric     Imp.Count = Item->getValue().size();
907ab83427SDimitry Andric     if (auto EC = Writer.writeObject(Imp))
917ab83427SDimitry Andric       return EC;
92e3b55780SDimitry Andric     if (auto EC = Writer.writeArray(ArrayRef(Item->getValue())))
937ab83427SDimitry Andric       return EC;
947ab83427SDimitry Andric   }
957ab83427SDimitry Andric   return Error::success();
967ab83427SDimitry Andric }
97