blob: c398ff0cec694ae95482f3f16d313f4ab51412bb [file] [log] [blame]
Eugene Zelenko66f724e2017-11-01 21:16:06 +00001//===- NonRelocatableStringpool.h - A simple stringpool --------*- C++ -*-===//
Frederic Rissfe893832015-08-26 05:09:52 +00002//
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 Zelenko66f724e2017-11-01 21:16:06 +00009
Frederic Rissfe893832015-08-26 05:09:52 +000010#ifndef LLVM_TOOLS_DSYMUTIL_NONRELOCATABLESTRINGPOOL_H
11#define LLVM_TOOLS_DSYMUTIL_NONRELOCATABLESTRINGPOOL_H
12
Jonas Devlieghere20767d12019-01-07 23:27:25 +000013#include "SymbolMap.h"
14
Benjamin Kramerb8c1bbf2016-01-27 19:29:56 +000015#include "llvm/ADT/StringMap.h"
Eugene Zelenko66f724e2017-11-01 21:16:06 +000016#include "llvm/ADT/StringRef.h"
Jonas Devlieghereef0ae3d2018-01-24 16:16:43 +000017#include "llvm/CodeGen/DwarfStringPoolEntry.h"
Eugene Zelenko66f724e2017-11-01 21:16:06 +000018#include "llvm/Support/Allocator.h"
19#include <cstdint>
Jonas Devliegheree993fb52018-03-01 10:05:54 +000020#include <vector>
Benjamin Kramerb8c1bbf2016-01-27 19:29:56 +000021
Frederic Rissfe893832015-08-26 05:09:52 +000022namespace llvm {
23namespace dsymutil {
24
Jonas Devlieghereef0ae3d2018-01-24 16:16:43 +000025/// A string table that doesn't need relocations.
Frederic Rissfe893832015-08-26 05:09:52 +000026///
Jonas Devlieghereef0ae3d2018-01-24 16:16:43 +000027/// We are doing a final link, no need for a string table that has relocation
Jonas Devlieghere3ad0c5a2018-02-22 11:32:51 +000028/// entries for every reference to it. This class provides this ability by just
29/// associating offsets with strings.
Frederic Rissfe893832015-08-26 05:09:52 +000030class NonRelocatableStringpool {
31public:
Jonas Devlieghereef0ae3d2018-01-24 16:16:43 +000032 /// 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 Rissfe893832015-08-26 05:09:52 +000036
Jonas Devlieghere20767d12019-01-07 23:27:25 +000037 NonRelocatableStringpool(
38 SymbolMapTranslator Translator = SymbolMapTranslator())
39 : Translator(Translator) {
Jonas Devlieghere3ad0c5a2018-02-22 11:32:51 +000040 // Legacy dsymutil puts an empty string at the start of the line table.
Jonas Devlieghereef0ae3d2018-01-24 16:16:43 +000041 EmptyString = getEntry("");
Frederic Rissfe893832015-08-26 05:09:52 +000042 }
43
Jonas Devlieghereef0ae3d2018-01-24 16:16:43 +000044 DwarfStringPoolEntryRef getEntry(StringRef S);
45
46 /// Get the offset of string \p S in the string table. This can insert a new
Jonas Devlieghere3ad0c5a2018-02-22 11:32:51 +000047 /// element or return the offset of a pre-existing one.
Jonas Devliegheree993fb52018-03-01 10:05:54 +000048 uint32_t getStringOffset(StringRef S) { return getEntry(S).getOffset(); }
Frederic Rissfe893832015-08-26 05:09:52 +000049
Jonas Devlieghereef0ae3d2018-01-24 16:16:43 +000050 /// Get permanent storage for \p S (but do not necessarily emit \p S in the
Jonas Devliegheree993fb52018-03-01 10:05:54 +000051 /// output section). A latter call to getStringOffset() with the same string
52 /// will chain it though.
53 ///
Frederic Rissfe893832015-08-26 05:09:52 +000054 /// \returns The StringRef that points to permanent storage to use
55 /// in place of \p S.
56 StringRef internString(StringRef S);
57
Frederic Rissfe893832015-08-26 05:09:52 +000058 uint64_t getSize() { return CurrentEndOffset; }
59
Pavel Labath23332c52018-08-07 09:54:52 +000060 /// 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 Devlieghereef0ae3d2018-01-24 16:16:43 +000063
Frederic Rissfe893832015-08-26 05:09:52 +000064private:
65 MapTy Strings;
Eugene Zelenko66f724e2017-11-01 21:16:06 +000066 uint32_t CurrentEndOffset = 0;
Jonas Devlieghereef0ae3d2018-01-24 16:16:43 +000067 unsigned NumEntries = 0;
68 DwarfStringPoolEntryRef EmptyString;
Jonas Devlieghere20767d12019-01-07 23:27:25 +000069 SymbolMapTranslator Translator;
Frederic Rissfe893832015-08-26 05:09:52 +000070};
Frederic Rissfe893832015-08-26 05:09:52 +000071
Jonas Devlieghere928fea22018-06-27 16:13:40 +000072/// Helper for making strong types.
73template <typename T, typename S> class StrongType : public T {
74public:
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.
82struct UniqueTag {};
83struct OffsetsTag {};
84using UniquingStringPool = StrongType<NonRelocatableStringpool, UniqueTag>;
85using OffsetsStringPool = StrongType<NonRelocatableStringpool, OffsetsTag>;
86
Eugene Zelenko66f724e2017-11-01 21:16:06 +000087} // end namespace dsymutil
88} // end namespace llvm
89
90#endif // LLVM_TOOLS_DSYMUTIL_NONRELOCATABLESTRINGPOOL_H