10b57cec5SDimitry Andric //===- DebugCrossExSubsection.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/DebugCrossExSubsection.h"
100b57cec5SDimitry Andric #include "llvm/DebugInfo/CodeView/CodeViewError.h"
1181ad6265SDimitry Andric #include "llvm/Support/BinaryStreamReader.h"
120b57cec5SDimitry Andric #include "llvm/Support/BinaryStreamWriter.h"
130b57cec5SDimitry Andric #include "llvm/Support/Error.h"
140b57cec5SDimitry Andric #include <cstdint>
150b57cec5SDimitry Andric 
160b57cec5SDimitry Andric using namespace llvm;
170b57cec5SDimitry Andric using namespace llvm::codeview;
180b57cec5SDimitry Andric 
initialize(BinaryStreamReader Reader)190b57cec5SDimitry Andric Error DebugCrossModuleExportsSubsectionRef::initialize(
200b57cec5SDimitry Andric     BinaryStreamReader Reader) {
210b57cec5SDimitry Andric   if (Reader.bytesRemaining() % sizeof(CrossModuleExport) != 0)
220b57cec5SDimitry Andric     return make_error<CodeViewError>(
230b57cec5SDimitry Andric         cv_error_code::corrupt_record,
240b57cec5SDimitry Andric         "Cross Scope Exports section is an invalid size!");
250b57cec5SDimitry Andric 
260b57cec5SDimitry Andric   uint32_t Size = Reader.bytesRemaining() / sizeof(CrossModuleExport);
270b57cec5SDimitry Andric   return Reader.readArray(References, Size);
280b57cec5SDimitry Andric }
290b57cec5SDimitry Andric 
initialize(BinaryStreamRef Stream)300b57cec5SDimitry Andric Error DebugCrossModuleExportsSubsectionRef::initialize(BinaryStreamRef Stream) {
310b57cec5SDimitry Andric   BinaryStreamReader Reader(Stream);
320b57cec5SDimitry Andric   return initialize(Reader);
330b57cec5SDimitry Andric }
340b57cec5SDimitry Andric 
addMapping(uint32_t Local,uint32_t Global)350b57cec5SDimitry Andric void DebugCrossModuleExportsSubsection::addMapping(uint32_t Local,
360b57cec5SDimitry Andric                                                    uint32_t Global) {
370b57cec5SDimitry Andric   Mappings[Local] = Global;
380b57cec5SDimitry Andric }
390b57cec5SDimitry Andric 
calculateSerializedSize() const400b57cec5SDimitry Andric uint32_t DebugCrossModuleExportsSubsection::calculateSerializedSize() const {
410b57cec5SDimitry Andric   return Mappings.size() * sizeof(CrossModuleExport);
420b57cec5SDimitry Andric }
430b57cec5SDimitry Andric 
commit(BinaryStreamWriter & Writer) const440b57cec5SDimitry Andric Error DebugCrossModuleExportsSubsection::commit(
450b57cec5SDimitry Andric     BinaryStreamWriter &Writer) const {
460b57cec5SDimitry Andric   for (const auto &M : Mappings) {
470b57cec5SDimitry Andric     if (auto EC = Writer.writeInteger(M.first))
480b57cec5SDimitry Andric       return EC;
490b57cec5SDimitry Andric     if (auto EC = Writer.writeInteger(M.second))
500b57cec5SDimitry Andric       return EC;
510b57cec5SDimitry Andric   }
520b57cec5SDimitry Andric   return Error::success();
530b57cec5SDimitry Andric }
54