Eugene Zelenko | 66f724e | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 1 | //===- NonRelocatableStringpool.h - A simple stringpool --------*- C++ -*-===// |
Frederic Riss | fe89383 | 2015-08-26 05:09:52 +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 | //===----------------------------------------------------------------------===// |
Eugene Zelenko | 66f724e | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 9 | |
Frederic Riss | fe89383 | 2015-08-26 05:09:52 +0000 | [diff] [blame] | 10 | #ifndef LLVM_TOOLS_DSYMUTIL_NONRELOCATABLESTRINGPOOL_H |
| 11 | #define LLVM_TOOLS_DSYMUTIL_NONRELOCATABLESTRINGPOOL_H |
| 12 | |
Jonas Devlieghere | 20767d1 | 2019-01-07 23:27:25 +0000 | [diff] [blame] | 13 | #include "SymbolMap.h" |
| 14 | |
Benjamin Kramer | b8c1bbf | 2016-01-27 19:29:56 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/StringMap.h" |
Eugene Zelenko | 66f724e | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/StringRef.h" |
Jonas Devlieghere | ef0ae3d | 2018-01-24 16:16:43 +0000 | [diff] [blame] | 17 | #include "llvm/CodeGen/DwarfStringPoolEntry.h" |
Eugene Zelenko | 66f724e | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 18 | #include "llvm/Support/Allocator.h" |
| 19 | #include <cstdint> |
Jonas Devlieghere | e993fb5 | 2018-03-01 10:05:54 +0000 | [diff] [blame] | 20 | #include <vector> |
Benjamin Kramer | b8c1bbf | 2016-01-27 19:29:56 +0000 | [diff] [blame] | 21 | |
Frederic Riss | fe89383 | 2015-08-26 05:09:52 +0000 | [diff] [blame] | 22 | namespace llvm { |
| 23 | namespace dsymutil { |
| 24 | |
Jonas Devlieghere | ef0ae3d | 2018-01-24 16:16:43 +0000 | [diff] [blame] | 25 | /// A string table that doesn't need relocations. |
Frederic Riss | fe89383 | 2015-08-26 05:09:52 +0000 | [diff] [blame] | 26 | /// |
Jonas Devlieghere | ef0ae3d | 2018-01-24 16:16:43 +0000 | [diff] [blame] | 27 | /// We are doing a final link, no need for a string table that has relocation |
Jonas Devlieghere | 3ad0c5a | 2018-02-22 11:32:51 +0000 | [diff] [blame] | 28 | /// entries for every reference to it. This class provides this ability by just |
| 29 | /// associating offsets with strings. |
Frederic Riss | fe89383 | 2015-08-26 05:09:52 +0000 | [diff] [blame] | 30 | class NonRelocatableStringpool { |
| 31 | public: |
Jonas Devlieghere | ef0ae3d | 2018-01-24 16:16:43 +0000 | [diff] [blame] | 32 | /// Entries are stored into the StringMap and simply linked together through |
| 33 | /// the second element of this pair in order to keep track of insertion |
| 34 | /// order. |
| 35 | using MapTy = StringMap<DwarfStringPoolEntry, BumpPtrAllocator>; |
Frederic Riss | fe89383 | 2015-08-26 05:09:52 +0000 | [diff] [blame] | 36 | |
Jonas Devlieghere | 20767d1 | 2019-01-07 23:27:25 +0000 | [diff] [blame] | 37 | NonRelocatableStringpool( |
| 38 | SymbolMapTranslator Translator = SymbolMapTranslator()) |
| 39 | : Translator(Translator) { |
Jonas Devlieghere | 3ad0c5a | 2018-02-22 11:32:51 +0000 | [diff] [blame] | 40 | // Legacy dsymutil puts an empty string at the start of the line table. |
Jonas Devlieghere | ef0ae3d | 2018-01-24 16:16:43 +0000 | [diff] [blame] | 41 | EmptyString = getEntry(""); |
Frederic Riss | fe89383 | 2015-08-26 05:09:52 +0000 | [diff] [blame] | 42 | } |
| 43 | |
Jonas Devlieghere | ef0ae3d | 2018-01-24 16:16:43 +0000 | [diff] [blame] | 44 | DwarfStringPoolEntryRef getEntry(StringRef S); |
| 45 | |
| 46 | /// Get the offset of string \p S in the string table. This can insert a new |
Jonas Devlieghere | 3ad0c5a | 2018-02-22 11:32:51 +0000 | [diff] [blame] | 47 | /// element or return the offset of a pre-existing one. |
Jonas Devlieghere | e993fb5 | 2018-03-01 10:05:54 +0000 | [diff] [blame] | 48 | uint32_t getStringOffset(StringRef S) { return getEntry(S).getOffset(); } |
Frederic Riss | fe89383 | 2015-08-26 05:09:52 +0000 | [diff] [blame] | 49 | |
Jonas Devlieghere | ef0ae3d | 2018-01-24 16:16:43 +0000 | [diff] [blame] | 50 | /// Get permanent storage for \p S (but do not necessarily emit \p S in the |
Jonas Devlieghere | e993fb5 | 2018-03-01 10:05:54 +0000 | [diff] [blame] | 51 | /// output section). A latter call to getStringOffset() with the same string |
| 52 | /// will chain it though. |
| 53 | /// |
Frederic Riss | fe89383 | 2015-08-26 05:09:52 +0000 | [diff] [blame] | 54 | /// \returns The StringRef that points to permanent storage to use |
| 55 | /// in place of \p S. |
| 56 | StringRef internString(StringRef S); |
| 57 | |
Frederic Riss | fe89383 | 2015-08-26 05:09:52 +0000 | [diff] [blame] | 58 | uint64_t getSize() { return CurrentEndOffset; } |
| 59 | |
Pavel Labath | 23332c5 | 2018-08-07 09:54:52 +0000 | [diff] [blame] | 60 | /// Return the list of strings to be emitted. This does not contain the |
| 61 | /// strings which were added via internString only. |
| 62 | std::vector<DwarfStringPoolEntryRef> getEntriesForEmission() const; |
Jonas Devlieghere | ef0ae3d | 2018-01-24 16:16:43 +0000 | [diff] [blame] | 63 | |
Frederic Riss | fe89383 | 2015-08-26 05:09:52 +0000 | [diff] [blame] | 64 | private: |
| 65 | MapTy Strings; |
Eugene Zelenko | 66f724e | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 66 | uint32_t CurrentEndOffset = 0; |
Jonas Devlieghere | ef0ae3d | 2018-01-24 16:16:43 +0000 | [diff] [blame] | 67 | unsigned NumEntries = 0; |
| 68 | DwarfStringPoolEntryRef EmptyString; |
Jonas Devlieghere | 20767d1 | 2019-01-07 23:27:25 +0000 | [diff] [blame] | 69 | SymbolMapTranslator Translator; |
Frederic Riss | fe89383 | 2015-08-26 05:09:52 +0000 | [diff] [blame] | 70 | }; |
Frederic Riss | fe89383 | 2015-08-26 05:09:52 +0000 | [diff] [blame] | 71 | |
Jonas Devlieghere | 928fea2 | 2018-06-27 16:13:40 +0000 | [diff] [blame] | 72 | /// Helper for making strong types. |
| 73 | template <typename T, typename S> class StrongType : public T { |
| 74 | public: |
| 75 | template <typename... Args> |
| 76 | explicit StrongType(Args... A) : T(std::forward<Args>(A)...) {} |
| 77 | }; |
| 78 | |
| 79 | /// It's very easy to introduce bugs by passing the wrong string pool in the |
| 80 | /// dwarf linker. By using strong types the interface enforces that the right |
| 81 | /// kind of pool is used. |
| 82 | struct UniqueTag {}; |
| 83 | struct OffsetsTag {}; |
| 84 | using UniquingStringPool = StrongType<NonRelocatableStringpool, UniqueTag>; |
| 85 | using OffsetsStringPool = StrongType<NonRelocatableStringpool, OffsetsTag>; |
| 86 | |
Eugene Zelenko | 66f724e | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 87 | } // end namespace dsymutil |
| 88 | } // end namespace llvm |
| 89 | |
| 90 | #endif // LLVM_TOOLS_DSYMUTIL_NONRELOCATABLESTRINGPOOL_H |