| 1 | //===- StringTableBuilder.cpp - String table building utility -------------===// |
| 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 | #include "llvm/MC/StringTableBuilder.h" |
| 10 | #include "llvm/ADT/ArrayRef.h" |
| 11 | #include "llvm/ADT/CachedHashString.h" |
| 12 | #include "llvm/ADT/SmallString.h" |
| 13 | #include "llvm/ADT/StringRef.h" |
| 14 | #include "llvm/Support/Endian.h" |
| 15 | #include "llvm/Support/MathExtras.h" |
| 16 | #include "llvm/Support/raw_ostream.h" |
| 17 | #include <cassert> |
| 18 | #include <cstddef> |
| 19 | #include <cstdint> |
| 20 | #include <cstring> |
| 21 | #include <utility> |
| 22 | #include <vector> |
| 23 | |
| 24 | using namespace llvm; |
| 25 | |
| 26 | StringTableBuilder::~StringTableBuilder() = default; |
| 27 | |
| 28 | void StringTableBuilder::initSize() { |
| 29 | // Account for leading bytes in table so that offsets returned from add are |
| 30 | // correct. |
| 31 | switch (K) { |
| 32 | case RAW: |
| 33 | case DWARF: |
| 34 | Size = 0; |
| 35 | break; |
| 36 | case MachOLinked: |
| 37 | case MachO64Linked: |
| 38 | Size = 2; |
| 39 | break; |
| 40 | case MachO: |
| 41 | case MachO64: |
| 42 | case ELF: |
| 43 | case DXContainer: |
| 44 | // Start the table with a NUL byte. |
| 45 | Size = 1; |
| 46 | break; |
| 47 | case XCOFF: |
| 48 | case WinCOFF: |
| 49 | // Make room to write the table size later. |
| 50 | Size = 4; |
| 51 | break; |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | StringTableBuilder::StringTableBuilder(Kind K, Align Alignment) |
| 56 | : K(K), Alignment(Alignment) { |
| 57 | initSize(); |
| 58 | } |
| 59 | |
| 60 | void StringTableBuilder::write(raw_ostream &OS) const { |
| 61 | assert(isFinalized()); |
| 62 | SmallString<0> Data; |
| 63 | Data.resize(N: getSize()); |
| 64 | write(Buf: (uint8_t *)Data.data()); |
| 65 | OS << Data; |
| 66 | } |
| 67 | |
| 68 | using StringPair = DenseMap<CachedHashStringRef, size_t>::value_type; |
| 69 | |
| 70 | void StringTableBuilder::write(uint8_t *Buf) const { |
| 71 | assert(isFinalized()); |
| 72 | for (const StringPair &P : StringIndexMap) { |
| 73 | StringRef Data = P.first.val(); |
| 74 | if (!Data.empty()) |
| 75 | memcpy(dest: Buf + P.second, src: Data.data(), n: Data.size()); |
| 76 | } |
| 77 | // The COFF formats store the size of the string table in the first 4 bytes. |
| 78 | // For Windows, the format is little-endian; for AIX, it is big-endian. |
| 79 | if (K == WinCOFF) |
| 80 | support::endian::write32le(P: Buf, V: Size); |
| 81 | else if (K == XCOFF) |
| 82 | support::endian::write32be(P: Buf, V: Size); |
| 83 | } |
| 84 | |
| 85 | // Returns the character at Pos from end of a string. |
| 86 | static int charTailAt(StringPair *P, size_t Pos) { |
| 87 | StringRef S = P->first.val(); |
| 88 | if (Pos >= S.size()) |
| 89 | return -1; |
| 90 | return (unsigned char)S[S.size() - Pos - 1]; |
| 91 | } |
| 92 | |
| 93 | // Three-way radix quicksort. This is much faster than std::sort with strcmp |
| 94 | // because it does not compare characters that we already know the same. |
| 95 | static void multikeySort(MutableArrayRef<StringPair *> Vec, int Pos) { |
| 96 | tailcall: |
| 97 | if (Vec.size() <= 1) |
| 98 | return; |
| 99 | |
| 100 | // Partition items so that items in [0, I) are greater than the pivot, |
| 101 | // [I, J) are the same as the pivot, and [J, Vec.size()) are less than |
| 102 | // the pivot. |
| 103 | int Pivot = charTailAt(P: Vec[0], Pos); |
| 104 | size_t I = 0; |
| 105 | size_t J = Vec.size(); |
| 106 | for (size_t K = 1; K < J;) { |
| 107 | int C = charTailAt(P: Vec[K], Pos); |
| 108 | if (C > Pivot) |
| 109 | std::swap(a&: Vec[I++], b&: Vec[K++]); |
| 110 | else if (C < Pivot) |
| 111 | std::swap(a&: Vec[--J], b&: Vec[K]); |
| 112 | else |
| 113 | K++; |
| 114 | } |
| 115 | |
| 116 | multikeySort(Vec: Vec.slice(N: 0, M: I), Pos); |
| 117 | multikeySort(Vec: Vec.slice(N: J), Pos); |
| 118 | |
| 119 | // multikeySort(Vec.slice(I, J - I), Pos + 1), but with |
| 120 | // tail call optimization. |
| 121 | if (Pivot != -1) { |
| 122 | Vec = Vec.slice(N: I, M: J - I); |
| 123 | ++Pos; |
| 124 | goto tailcall; |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | void StringTableBuilder::finalize() { |
| 129 | assert(K != DWARF); |
| 130 | finalizeStringTable(/*Optimize=*/true); |
| 131 | } |
| 132 | |
| 133 | void StringTableBuilder::finalizeInOrder() { |
| 134 | finalizeStringTable(/*Optimize=*/false); |
| 135 | } |
| 136 | |
| 137 | void StringTableBuilder::finalizeStringTable(bool Optimize) { |
| 138 | Finalized = true; |
| 139 | |
| 140 | if (Optimize && StringIndexMap.size()) { |
| 141 | std::vector<StringPair *> Strings; |
| 142 | Strings.reserve(n: StringIndexMap.size()); |
| 143 | for (StringPair &P : StringIndexMap) |
| 144 | Strings.push_back(x: &P); |
| 145 | |
| 146 | size_t RangeBegin = 0; |
| 147 | MutableArrayRef<StringPair *> StringsRef(Strings); |
| 148 | if (StringPriorityMap.size()) { |
| 149 | llvm::sort(C&: Strings, |
| 150 | Comp: [&](const StringPair *LHS, const StringPair *RHS) -> bool { |
| 151 | return StringPriorityMap.lookup(Val: LHS->first) > |
| 152 | StringPriorityMap.lookup(Val: RHS->first); |
| 153 | }); |
| 154 | uint8_t RangePriority = StringPriorityMap.lookup(Val: Strings[0]->first); |
| 155 | for (size_t I = 1, E = Strings.size(); I != E && RangePriority; ++I) { |
| 156 | uint8_t Priority = StringPriorityMap.lookup(Val: Strings[I]->first); |
| 157 | if (Priority != RangePriority) { |
| 158 | multikeySort(Vec: StringsRef.slice(N: RangeBegin, M: I - RangeBegin), Pos: 0); |
| 159 | RangePriority = Priority; |
| 160 | RangeBegin = I; |
| 161 | } |
| 162 | } |
| 163 | } |
| 164 | multikeySort(Vec: StringsRef.slice(N: RangeBegin), Pos: 0); |
| 165 | initSize(); |
| 166 | |
| 167 | StringRef Previous; |
| 168 | for (StringPair *P : Strings) { |
| 169 | StringRef S = P->first.val(); |
| 170 | if (Previous.ends_with(Suffix: S)) { |
| 171 | size_t Pos = Size - S.size() - (K != RAW); |
| 172 | if (isAligned(Lhs: Alignment, SizeInBytes: Pos)) { |
| 173 | P->second = Pos; |
| 174 | continue; |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | Size = alignTo(Size, A: Alignment); |
| 179 | P->second = Size; |
| 180 | |
| 181 | Size += S.size(); |
| 182 | if (K != RAW) |
| 183 | ++Size; |
| 184 | Previous = S; |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | if (K == MachO || K == MachOLinked || K == DXContainer) |
| 189 | Size = alignTo(Value: Size, Align: 4); // Pad to multiple of 4. |
| 190 | if (K == MachO64 || K == MachO64Linked) |
| 191 | Size = alignTo(Value: Size, Align: 8); // Pad to multiple of 8. |
| 192 | |
| 193 | // According to ld64 the string table of a final linked Mach-O binary starts |
| 194 | // with " ", i.e. the first byte is ' ' and the second byte is zero. In |
| 195 | // 'initSize()' we reserved the first two bytes for holding this string. |
| 196 | if (K == MachOLinked || K == MachO64Linked) |
| 197 | StringIndexMap[CachedHashStringRef(" " )] = 0; |
| 198 | |
| 199 | // The first byte in an ELF string table must be null, according to the ELF |
| 200 | // specification. In 'initSize()' we reserved the first byte to hold null for |
| 201 | // this purpose and here we actually add the string to allow 'getOffset()' to |
| 202 | // be called on an empty string. |
| 203 | if (K == ELF || K == DXContainer) |
| 204 | StringIndexMap[CachedHashStringRef("" )] = 0; |
| 205 | } |
| 206 | |
| 207 | void StringTableBuilder::clear() { |
| 208 | Finalized = false; |
| 209 | StringIndexMap.clear(); |
| 210 | } |
| 211 | |
| 212 | size_t StringTableBuilder::getOffset(CachedHashStringRef S) const { |
| 213 | assert(isFinalized()); |
| 214 | auto I = StringIndexMap.find(Val: S); |
| 215 | assert(I != StringIndexMap.end() && "String is not in table!" ); |
| 216 | return I->second; |
| 217 | } |
| 218 | |
| 219 | |