| 1 | //===- StringTable.h --------------------------------------------*- C++ -*-===// |
|---|---|
| 2 | // |
| 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 |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #ifndef LLVM_DEBUGINFO_GSYM_STRINGTABLE_H |
| 10 | #define LLVM_DEBUGINFO_GSYM_STRINGTABLE_H |
| 11 | |
| 12 | #include "llvm/ADT/StringRef.h" |
| 13 | #include "llvm/DebugInfo/GSYM/ExtractRanges.h" |
| 14 | #include "llvm/DebugInfo/GSYM/GsymTypes.h" |
| 15 | #include <stdint.h> |
| 16 | |
| 17 | namespace llvm { |
| 18 | namespace gsym { |
| 19 | |
| 20 | /// String tables in GSYM files are required to start with an empty |
| 21 | /// string at offset zero. Strings must be UTF8 NULL terminated strings. |
| 22 | struct StringTable { |
| 23 | StringRef Data; |
| 24 | StringTable() = default; |
| 25 | StringTable(StringRef D) : Data(D) {} |
| 26 | StringRef operator[](size_t Offset) const { return getString(Offset); } |
| 27 | StringRef getString(gsym_strp_t Offset) const { |
| 28 | if (Offset < Data.size()) { |
| 29 | auto End = Data.find(C: '\0', From: Offset); |
| 30 | return Data.substr(Start: Offset, N: End - Offset); |
| 31 | } |
| 32 | return StringRef(); |
| 33 | } |
| 34 | void clear() { Data = StringRef(); } |
| 35 | }; |
| 36 | |
| 37 | inline void dump(raw_ostream &OS, const StringTable &S, |
| 38 | uint8_t StringOffsetSize) { |
| 39 | OS << "String table:\n"; |
| 40 | gsym_strp_t Offset = 0; |
| 41 | const size_t Size = S.Data.size(); |
| 42 | while (Offset < Size) { |
| 43 | StringRef Str = S.getString(Offset); |
| 44 | switch (StringOffsetSize) { |
| 45 | case 1: |
| 46 | OS << HEX8(Offset); |
| 47 | break; |
| 48 | case 2: |
| 49 | OS << HEX16(Offset); |
| 50 | break; |
| 51 | case 4: |
| 52 | OS << HEX32(Offset); |
| 53 | break; |
| 54 | case 8: |
| 55 | OS << HEX64(Offset); |
| 56 | break; |
| 57 | default: |
| 58 | OS << HEX64(Offset); |
| 59 | } |
| 60 | OS << ": \""<< Str << "\"\n"; |
| 61 | Offset += Str.size() + 1; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | } // namespace gsym |
| 66 | } // namespace llvm |
| 67 | #endif // LLVM_DEBUGINFO_GSYM_STRINGTABLE_H |
| 68 |