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/CachedHashString.h" 11 #include "llvm/ADT/SmallString.h" 12 #include "llvm/ADT/StringRef.h" 13 #include "llvm/BinaryFormat/COFF.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 MachO: 37 case ELF: 38 // Start the table with a NUL byte. 39 Size = 1; 40 break; 41 case WinCOFF: 42 // Make room to write the table size later. 43 Size = 4; 44 break; 45 } 46 } 47 48 StringTableBuilder::StringTableBuilder(Kind K, unsigned Alignment) 49 : K(K), Alignment(Alignment) { 50 initSize(); 51 } 52 53 void StringTableBuilder::write(raw_ostream &OS) const { 54 assert(isFinalized()); 55 SmallString<0> Data; 56 Data.resize(getSize()); 57 write((uint8_t *)Data.data()); 58 OS << Data; 59 } 60 61 using StringPair = std::pair<CachedHashStringRef, size_t>; 62 63 void StringTableBuilder::write(uint8_t *Buf) const { 64 assert(isFinalized()); 65 for (const StringPair &P : StringIndexMap) { 66 StringRef Data = P.first.val(); 67 if (!Data.empty()) 68 memcpy(Buf + P.second, Data.data(), Data.size()); 69 } 70 if (K != WinCOFF) 71 return; 72 support::endian::write32le(Buf, Size); 73 } 74 75 // Returns the character at Pos from end of a string. 76 static int charTailAt(StringPair *P, size_t Pos) { 77 StringRef S = P->first.val(); 78 if (Pos >= S.size()) 79 return -1; 80 return (unsigned char)S[S.size() - Pos - 1]; 81 } 82 83 // Three-way radix quicksort. This is much faster than std::sort with strcmp 84 // because it does not compare characters that we already know the same. 85 static void multikeySort(MutableArrayRef<StringPair *> Vec, int Pos) { 86 tailcall: 87 if (Vec.size() <= 1) 88 return; 89 90 // Partition items so that items in [0, I) are greater than the pivot, 91 // [I, J) are the same as the pivot, and [J, Vec.size()) are less than 92 // the pivot. 93 int Pivot = charTailAt(Vec[0], Pos); 94 size_t I = 0; 95 size_t J = Vec.size(); 96 for (size_t K = 1; K < J;) { 97 int C = charTailAt(Vec[K], Pos); 98 if (C > Pivot) 99 std::swap(Vec[I++], Vec[K++]); 100 else if (C < Pivot) 101 std::swap(Vec[--J], Vec[K]); 102 else 103 K++; 104 } 105 106 multikeySort(Vec.slice(0, I), Pos); 107 multikeySort(Vec.slice(J), Pos); 108 109 // multikeySort(Vec.slice(I, J - I), Pos + 1), but with 110 // tail call optimization. 111 if (Pivot != -1) { 112 Vec = Vec.slice(I, J - I); 113 ++Pos; 114 goto tailcall; 115 } 116 } 117 118 void StringTableBuilder::finalize() { 119 assert(K != DWARF); 120 finalizeStringTable(/*Optimize=*/true); 121 } 122 123 void StringTableBuilder::finalizeInOrder() { 124 finalizeStringTable(/*Optimize=*/false); 125 } 126 127 void StringTableBuilder::finalizeStringTable(bool Optimize) { 128 Finalized = true; 129 130 if (Optimize) { 131 std::vector<StringPair *> Strings; 132 Strings.reserve(StringIndexMap.size()); 133 for (StringPair &P : StringIndexMap) 134 Strings.push_back(&P); 135 136 multikeySort(Strings, 0); 137 initSize(); 138 139 StringRef Previous; 140 for (StringPair *P : Strings) { 141 StringRef S = P->first.val(); 142 if (Previous.endswith(S)) { 143 size_t Pos = Size - S.size() - (K != RAW); 144 if (!(Pos & (Alignment - 1))) { 145 P->second = Pos; 146 continue; 147 } 148 } 149 150 Size = alignTo(Size, Alignment); 151 P->second = Size; 152 153 Size += S.size(); 154 if (K != RAW) 155 ++Size; 156 Previous = S; 157 } 158 } 159 160 if (K == MachO) 161 Size = alignTo(Size, 4); // Pad to multiple of 4. 162 163 // The first byte in an ELF string table must be null, according to the ELF 164 // specification. In 'initSize()' we reserved the first byte to hold null for 165 // this purpose and here we actually add the string to allow 'getOffset()' to 166 // be called on an empty string. 167 if (K == ELF) 168 StringIndexMap[CachedHashStringRef("")] = 0; 169 } 170 171 void StringTableBuilder::clear() { 172 Finalized = false; 173 StringIndexMap.clear(); 174 } 175 176 size_t StringTableBuilder::getOffset(CachedHashStringRef S) const { 177 assert(isFinalized()); 178 auto I = StringIndexMap.find(S); 179 assert(I != StringIndexMap.end() && "String is not in table!"); 180 return I->second; 181 } 182 183 size_t StringTableBuilder::add(CachedHashStringRef S) { 184 if (K == WinCOFF) 185 assert(S.size() > COFF::NameSize && "Short string in COFF string table!"); 186 187 assert(!isFinalized()); 188 auto P = StringIndexMap.insert(std::make_pair(S, 0)); 189 if (P.second) { 190 size_t Start = alignTo(Size, Alignment); 191 P.first->second = Start; 192 Size = Start + S.size() + (K != RAW); 193 } 194 return P.first->second; 195 } 196