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