Eugene Zelenko | 8456b16 | 2017-06-29 00:05:44 +0000 | [diff] [blame] | 1 | //===- DebugStringTableSubsection.cpp - CodeView String Table -------------===// |
Zachary Turner | c504ae3 | 2017-05-03 15:58:37 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
Zachary Turner | 591312c | 2017-05-30 17:13:33 +0000 | [diff] [blame] | 10 | #include "llvm/DebugInfo/CodeView/DebugStringTableSubsection.h" |
Eugene Zelenko | 8456b16 | 2017-06-29 00:05:44 +0000 | [diff] [blame] | 11 | #include "llvm/ADT/StringRef.h" |
| 12 | #include "llvm/DebugInfo/CodeView/CodeView.h" |
Zachary Turner | c504ae3 | 2017-05-03 15:58:37 +0000 | [diff] [blame] | 13 | #include "llvm/Support/BinaryStreamReader.h" |
| 14 | #include "llvm/Support/BinaryStreamWriter.h" |
Eugene Zelenko | 8456b16 | 2017-06-29 00:05:44 +0000 | [diff] [blame] | 15 | #include "llvm/Support/Error.h" |
| 16 | #include <algorithm> |
| 17 | #include <cassert> |
| 18 | #include <cstdint> |
Zachary Turner | c504ae3 | 2017-05-03 15:58:37 +0000 | [diff] [blame] | 19 | |
| 20 | using namespace llvm; |
| 21 | using namespace llvm::codeview; |
| 22 | |
Zachary Turner | 591312c | 2017-05-30 17:13:33 +0000 | [diff] [blame] | 23 | DebugStringTableSubsectionRef::DebugStringTableSubsectionRef() |
| 24 | : DebugSubsectionRef(DebugSubsectionKind::StringTable) {} |
Zachary Turner | c504ae3 | 2017-05-03 15:58:37 +0000 | [diff] [blame] | 25 | |
Zachary Turner | 591312c | 2017-05-30 17:13:33 +0000 | [diff] [blame] | 26 | Error DebugStringTableSubsectionRef::initialize(BinaryStreamRef Contents) { |
Zachary Turner | 2d5c2cd | 2017-05-03 17:11:11 +0000 | [diff] [blame] | 27 | Stream = Contents; |
| 28 | return Error::success(); |
Zachary Turner | c504ae3 | 2017-05-03 15:58:37 +0000 | [diff] [blame] | 29 | } |
Eugene Zelenko | 8456b16 | 2017-06-29 00:05:44 +0000 | [diff] [blame] | 30 | |
Zachary Turner | 1bf7762 | 2017-06-08 23:49:01 +0000 | [diff] [blame] | 31 | Error DebugStringTableSubsectionRef::initialize(BinaryStreamReader &Reader) { |
Zachary Turner | deb3913 | 2017-06-09 00:28:08 +0000 | [diff] [blame] | 32 | return Reader.readStreamRef(Stream); |
Zachary Turner | 1bf7762 | 2017-06-08 23:49:01 +0000 | [diff] [blame] | 33 | } |
Zachary Turner | c504ae3 | 2017-05-03 15:58:37 +0000 | [diff] [blame] | 34 | |
Zachary Turner | 591312c | 2017-05-30 17:13:33 +0000 | [diff] [blame] | 35 | Expected<StringRef> |
| 36 | DebugStringTableSubsectionRef::getString(uint32_t Offset) const { |
Zachary Turner | c504ae3 | 2017-05-03 15:58:37 +0000 | [diff] [blame] | 37 | BinaryStreamReader Reader(Stream); |
| 38 | Reader.setOffset(Offset); |
| 39 | StringRef Result; |
Zachary Turner | 2d5c2cd | 2017-05-03 17:11:11 +0000 | [diff] [blame] | 40 | if (auto EC = Reader.readCString(Result)) |
| 41 | return std::move(EC); |
Zachary Turner | c504ae3 | 2017-05-03 15:58:37 +0000 | [diff] [blame] | 42 | return Result; |
| 43 | } |
| 44 | |
Zachary Turner | 591312c | 2017-05-30 17:13:33 +0000 | [diff] [blame] | 45 | DebugStringTableSubsection::DebugStringTableSubsection() |
| 46 | : DebugSubsection(DebugSubsectionKind::StringTable) {} |
| 47 | |
| 48 | uint32_t DebugStringTableSubsection::insert(StringRef S) { |
Zachary Turner | fced530 | 2018-03-20 18:37:03 +0000 | [diff] [blame] | 49 | auto P = Strings.insert({S, StringSize}); |
Zachary Turner | c504ae3 | 2017-05-03 15:58:37 +0000 | [diff] [blame] | 50 | |
| 51 | // If a given string didn't exist in the string table, we want to increment |
Zachary Turner | fced530 | 2018-03-20 18:37:03 +0000 | [diff] [blame] | 52 | // the string table size. |
| 53 | if (P.second) |
Zachary Turner | c504ae3 | 2017-05-03 15:58:37 +0000 | [diff] [blame] | 54 | StringSize += S.size() + 1; // +1 for '\0' |
| 55 | return P.first->second; |
| 56 | } |
| 57 | |
Zachary Turner | 591312c | 2017-05-30 17:13:33 +0000 | [diff] [blame] | 58 | uint32_t DebugStringTableSubsection::calculateSerializedSize() const { |
| 59 | return StringSize; |
| 60 | } |
Zachary Turner | c504ae3 | 2017-05-03 15:58:37 +0000 | [diff] [blame] | 61 | |
Zachary Turner | 591312c | 2017-05-30 17:13:33 +0000 | [diff] [blame] | 62 | Error DebugStringTableSubsection::commit(BinaryStreamWriter &Writer) const { |
Zachary Turner | deb3913 | 2017-06-09 00:28:08 +0000 | [diff] [blame] | 63 | uint32_t Begin = Writer.getOffset(); |
| 64 | uint32_t End = Begin + StringSize; |
Zachary Turner | c504ae3 | 2017-05-03 15:58:37 +0000 | [diff] [blame] | 65 | |
Zachary Turner | a8cfc29 | 2017-06-14 15:59:27 +0000 | [diff] [blame] | 66 | // Write a null string at the beginning. |
| 67 | if (auto EC = Writer.writeCString(StringRef())) |
| 68 | return EC; |
| 69 | |
Zachary Turner | fced530 | 2018-03-20 18:37:03 +0000 | [diff] [blame] | 70 | for (auto &Pair : Strings) { |
Zachary Turner | c504ae3 | 2017-05-03 15:58:37 +0000 | [diff] [blame] | 71 | StringRef S = Pair.getKey(); |
Zachary Turner | deb3913 | 2017-06-09 00:28:08 +0000 | [diff] [blame] | 72 | uint32_t Offset = Begin + Pair.getValue(); |
Zachary Turner | c504ae3 | 2017-05-03 15:58:37 +0000 | [diff] [blame] | 73 | Writer.setOffset(Offset); |
| 74 | if (auto EC = Writer.writeCString(S)) |
| 75 | return EC; |
Zachary Turner | deb3913 | 2017-06-09 00:28:08 +0000 | [diff] [blame] | 76 | assert(Writer.getOffset() <= End); |
Zachary Turner | c504ae3 | 2017-05-03 15:58:37 +0000 | [diff] [blame] | 77 | } |
| 78 | |
Zachary Turner | deb3913 | 2017-06-09 00:28:08 +0000 | [diff] [blame] | 79 | Writer.setOffset(End); |
Zachary Turner | a8cfc29 | 2017-06-14 15:59:27 +0000 | [diff] [blame] | 80 | assert((End - Begin) == StringSize); |
Zachary Turner | c504ae3 | 2017-05-03 15:58:37 +0000 | [diff] [blame] | 81 | return Error::success(); |
| 82 | } |
| 83 | |
Zachary Turner | fced530 | 2018-03-20 18:37:03 +0000 | [diff] [blame] | 84 | uint32_t DebugStringTableSubsection::size() const { return Strings.size(); } |
Zachary Turner | cf468d8 | 2017-05-03 17:11:40 +0000 | [diff] [blame] | 85 | |
Zachary Turner | fced530 | 2018-03-20 18:37:03 +0000 | [diff] [blame] | 86 | uint32_t DebugStringTableSubsection::getStringId(StringRef S) const { |
| 87 | auto Iter = Strings.find(S); |
| 88 | assert(Iter != Strings.end()); |
Zachary Turner | 92dcdda | 2017-06-02 19:49:14 +0000 | [diff] [blame] | 89 | return Iter->second; |
Zachary Turner | cf468d8 | 2017-05-03 17:11:40 +0000 | [diff] [blame] | 90 | } |