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