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