xref: /freebsd-src/contrib/llvm-project/llvm/lib/DebugInfo/CodeView/DebugCrossImpSubsection.cpp (revision bdd1243df58e60e85101c09001d9812a789b6bc4)
10b57cec5SDimitry Andric //===- DebugCrossImpSubsection.cpp ----------------------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric 
90b57cec5SDimitry Andric #include "llvm/DebugInfo/CodeView/DebugCrossImpSubsection.h"
100b57cec5SDimitry Andric #include "llvm/ADT/ArrayRef.h"
110b57cec5SDimitry Andric #include "llvm/DebugInfo/CodeView/CodeViewError.h"
120b57cec5SDimitry Andric #include "llvm/DebugInfo/CodeView/DebugStringTableSubsection.h"
130b57cec5SDimitry Andric #include "llvm/Support/BinaryStreamReader.h"
140b57cec5SDimitry Andric #include "llvm/Support/BinaryStreamWriter.h"
150b57cec5SDimitry Andric #include "llvm/Support/Endian.h"
160b57cec5SDimitry Andric #include "llvm/Support/Error.h"
170b57cec5SDimitry Andric #include <algorithm>
180b57cec5SDimitry Andric #include <cstdint>
190b57cec5SDimitry Andric #include <utility>
200b57cec5SDimitry Andric #include <vector>
210b57cec5SDimitry Andric 
220b57cec5SDimitry Andric using namespace llvm;
230b57cec5SDimitry Andric using namespace llvm::codeview;
240b57cec5SDimitry Andric 
250b57cec5SDimitry Andric Error VarStreamArrayExtractor<CrossModuleImportItem>::
operator ()(BinaryStreamRef Stream,uint32_t & Len,codeview::CrossModuleImportItem & Item)260b57cec5SDimitry Andric operator()(BinaryStreamRef Stream, uint32_t &Len,
270b57cec5SDimitry Andric            codeview::CrossModuleImportItem &Item) {
280b57cec5SDimitry Andric   BinaryStreamReader Reader(Stream);
290b57cec5SDimitry Andric   if (Reader.bytesRemaining() < sizeof(CrossModuleImport))
300b57cec5SDimitry Andric     return make_error<CodeViewError>(
310b57cec5SDimitry Andric         cv_error_code::insufficient_buffer,
320b57cec5SDimitry Andric         "Not enough bytes for a Cross Module Import Header!");
330b57cec5SDimitry Andric   if (auto EC = Reader.readObject(Item.Header))
340b57cec5SDimitry Andric     return EC;
350b57cec5SDimitry Andric   if (Reader.bytesRemaining() < Item.Header->Count * sizeof(uint32_t))
360b57cec5SDimitry Andric     return make_error<CodeViewError>(
370b57cec5SDimitry Andric         cv_error_code::insufficient_buffer,
380b57cec5SDimitry Andric         "Not enough to read specified number of Cross Module References!");
390b57cec5SDimitry Andric   if (auto EC = Reader.readArray(Item.Imports, Item.Header->Count))
400b57cec5SDimitry Andric     return EC;
410b57cec5SDimitry Andric   return Error::success();
420b57cec5SDimitry Andric }
430b57cec5SDimitry Andric 
initialize(BinaryStreamReader Reader)440b57cec5SDimitry Andric Error DebugCrossModuleImportsSubsectionRef::initialize(
450b57cec5SDimitry Andric     BinaryStreamReader Reader) {
460b57cec5SDimitry Andric   return Reader.readArray(References, Reader.bytesRemaining());
470b57cec5SDimitry Andric }
480b57cec5SDimitry Andric 
initialize(BinaryStreamRef Stream)490b57cec5SDimitry Andric Error DebugCrossModuleImportsSubsectionRef::initialize(BinaryStreamRef Stream) {
500b57cec5SDimitry Andric   BinaryStreamReader Reader(Stream);
510b57cec5SDimitry Andric   return initialize(Reader);
520b57cec5SDimitry Andric }
530b57cec5SDimitry Andric 
addImport(StringRef Module,uint32_t ImportId)540b57cec5SDimitry Andric void DebugCrossModuleImportsSubsection::addImport(StringRef Module,
550b57cec5SDimitry Andric                                                   uint32_t ImportId) {
560b57cec5SDimitry Andric   Strings.insert(Module);
570b57cec5SDimitry Andric   std::vector<support::ulittle32_t> Targets = {support::ulittle32_t(ImportId)};
580b57cec5SDimitry Andric   auto Result = Mappings.insert(std::make_pair(Module, Targets));
590b57cec5SDimitry Andric   if (!Result.second)
600b57cec5SDimitry Andric     Result.first->getValue().push_back(Targets[0]);
610b57cec5SDimitry Andric }
620b57cec5SDimitry Andric 
calculateSerializedSize() const630b57cec5SDimitry Andric uint32_t DebugCrossModuleImportsSubsection::calculateSerializedSize() const {
640b57cec5SDimitry Andric   uint32_t Size = 0;
650b57cec5SDimitry Andric   for (const auto &Item : Mappings) {
660b57cec5SDimitry Andric     Size += sizeof(CrossModuleImport);
670b57cec5SDimitry Andric     Size += sizeof(support::ulittle32_t) * Item.second.size();
680b57cec5SDimitry Andric   }
690b57cec5SDimitry Andric   return Size;
700b57cec5SDimitry Andric }
710b57cec5SDimitry Andric 
commit(BinaryStreamWriter & Writer) const720b57cec5SDimitry Andric Error DebugCrossModuleImportsSubsection::commit(
730b57cec5SDimitry Andric     BinaryStreamWriter &Writer) const {
740b57cec5SDimitry Andric   using T = decltype(&*Mappings.begin());
750b57cec5SDimitry Andric   std::vector<T> Ids;
760b57cec5SDimitry Andric   Ids.reserve(Mappings.size());
770b57cec5SDimitry Andric 
780b57cec5SDimitry Andric   for (const auto &M : Mappings)
790b57cec5SDimitry Andric     Ids.push_back(&M);
800b57cec5SDimitry Andric 
810b57cec5SDimitry Andric   llvm::sort(Ids, [this](const T &L1, const T &L2) {
820b57cec5SDimitry Andric     return Strings.getIdForString(L1->getKey()) <
830b57cec5SDimitry Andric            Strings.getIdForString(L2->getKey());
840b57cec5SDimitry Andric   });
850b57cec5SDimitry Andric 
860b57cec5SDimitry Andric   for (const auto &Item : Ids) {
870b57cec5SDimitry Andric     CrossModuleImport Imp;
880b57cec5SDimitry Andric     Imp.ModuleNameOffset = Strings.getIdForString(Item->getKey());
890b57cec5SDimitry Andric     Imp.Count = Item->getValue().size();
900b57cec5SDimitry Andric     if (auto EC = Writer.writeObject(Imp))
910b57cec5SDimitry Andric       return EC;
92*bdd1243dSDimitry Andric     if (auto EC = Writer.writeArray(ArrayRef(Item->getValue())))
930b57cec5SDimitry Andric       return EC;
940b57cec5SDimitry Andric   }
950b57cec5SDimitry Andric   return Error::success();
960b57cec5SDimitry Andric }
97