StringTableBuilder.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. #include "llvm/MC/StringTableBuilder.h"
  9. #include "llvm/ADT/CachedHashString.h"
  10. #include "llvm/ADT/SmallString.h"
  11. #include "llvm/ADT/StringRef.h"
  12. #include "llvm/BinaryFormat/COFF.h"
  13. #include "llvm/Support/Endian.h"
  14. #include "llvm/Support/MathExtras.h"
  15. #include "llvm/Support/raw_ostream.h"
  16. #include <cassert>
  17. #include <cstddef>
  18. #include <cstdint>
  19. #include <cstring>
  20. #include <utility>
  21. #include <vector>
  22. using namespace llvm;
  23. StringTableBuilder::~StringTableBuilder() = default;
  24. void StringTableBuilder::initSize() {
  25. // Account for leading bytes in table so that offsets returned from add are
  26. // correct.
  27. switch (K) {
  28. case RAW:
  29. case DWARF:
  30. Size = 0;
  31. break;
  32. case MachOLinked:
  33. case MachO64Linked:
  34. Size = 2;
  35. break;
  36. case MachO:
  37. case MachO64:
  38. case ELF:
  39. // Start the table with a NUL byte.
  40. Size = 1;
  41. break;
  42. case XCOFF:
  43. case WinCOFF:
  44. // Make room to write the table size later.
  45. Size = 4;
  46. break;
  47. }
  48. }
  49. StringTableBuilder::StringTableBuilder(Kind K, unsigned Alignment)
  50. : K(K), Alignment(Alignment) {
  51. initSize();
  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. using StringPair = std::pair<CachedHashStringRef, size_t>;
  61. void StringTableBuilder::write(uint8_t *Buf) const {
  62. assert(isFinalized());
  63. for (const StringPair &P : StringIndexMap) {
  64. StringRef Data = P.first.val();
  65. if (!Data.empty())
  66. memcpy(Buf + P.second, Data.data(), Data.size());
  67. }
  68. // The COFF formats store the size of the string table in the first 4 bytes.
  69. // For Windows, the format is little-endian; for AIX, it is big-endian.
  70. if (K == WinCOFF)
  71. support::endian::write32le(Buf, Size);
  72. else if (K == XCOFF)
  73. support::endian::write32be(Buf, Size);
  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. // Three-way radix quicksort. This is much faster than std::sort with strcmp
  83. // because it does not compare characters that we already know the same.
  84. static void multikeySort(MutableArrayRef<StringPair *> Vec, int Pos) {
  85. tailcall:
  86. if (Vec.size() <= 1)
  87. return;
  88. // Partition items so that items in [0, I) are greater than the pivot,
  89. // [I, J) are the same as the pivot, and [J, Vec.size()) are less than
  90. // the pivot.
  91. int Pivot = charTailAt(Vec[0], Pos);
  92. size_t I = 0;
  93. size_t J = Vec.size();
  94. for (size_t K = 1; K < J;) {
  95. int C = charTailAt(Vec[K], Pos);
  96. if (C > Pivot)
  97. std::swap(Vec[I++], Vec[K++]);
  98. else if (C < Pivot)
  99. std::swap(Vec[--J], Vec[K]);
  100. else
  101. K++;
  102. }
  103. multikeySort(Vec.slice(0, I), Pos);
  104. multikeySort(Vec.slice(J), Pos);
  105. // multikeySort(Vec.slice(I, J - I), Pos + 1), but with
  106. // tail call optimization.
  107. if (Pivot != -1) {
  108. Vec = Vec.slice(I, J - I);
  109. ++Pos;
  110. goto tailcall;
  111. }
  112. }
  113. void StringTableBuilder::finalize() {
  114. assert(K != DWARF);
  115. finalizeStringTable(/*Optimize=*/true);
  116. }
  117. void StringTableBuilder::finalizeInOrder() {
  118. finalizeStringTable(/*Optimize=*/false);
  119. }
  120. void StringTableBuilder::finalizeStringTable(bool Optimize) {
  121. Finalized = true;
  122. if (Optimize) {
  123. std::vector<StringPair *> Strings;
  124. Strings.reserve(StringIndexMap.size());
  125. for (StringPair &P : StringIndexMap)
  126. Strings.push_back(&P);
  127. multikeySort(Strings, 0);
  128. initSize();
  129. StringRef Previous;
  130. for (StringPair *P : Strings) {
  131. StringRef S = P->first.val();
  132. if (Previous.endswith(S)) {
  133. size_t Pos = Size - S.size() - (K != RAW);
  134. if (!(Pos & (Alignment - 1))) {
  135. P->second = Pos;
  136. continue;
  137. }
  138. }
  139. Size = alignTo(Size, Alignment);
  140. P->second = Size;
  141. Size += S.size();
  142. if (K != RAW)
  143. ++Size;
  144. Previous = S;
  145. }
  146. }
  147. if (K == MachO || K == MachOLinked)
  148. Size = alignTo(Size, 4); // Pad to multiple of 4.
  149. if (K == MachO64 || K == MachO64Linked)
  150. Size = alignTo(Size, 8); // Pad to multiple of 8.
  151. // According to ld64 the string table of a final linked Mach-O binary starts
  152. // with " ", i.e. the first byte is ' ' and the second byte is zero. In
  153. // 'initSize()' we reserved the first two bytes for holding this string.
  154. if (K == MachOLinked || K == MachO64Linked)
  155. StringIndexMap[CachedHashStringRef(" ")] = 0;
  156. // The first byte in an ELF string table must be null, according to the ELF
  157. // specification. In 'initSize()' we reserved the first byte to hold null for
  158. // this purpose and here we actually add the string to allow 'getOffset()' to
  159. // be called on an empty string.
  160. if (K == ELF)
  161. StringIndexMap[CachedHashStringRef("")] = 0;
  162. }
  163. void StringTableBuilder::clear() {
  164. Finalized = false;
  165. StringIndexMap.clear();
  166. }
  167. size_t StringTableBuilder::getOffset(CachedHashStringRef S) const {
  168. assert(isFinalized());
  169. auto I = StringIndexMap.find(S);
  170. assert(I != StringIndexMap.end() && "String is not in table!");
  171. return I->second;
  172. }
  173. size_t StringTableBuilder::add(CachedHashStringRef S) {
  174. if (K == WinCOFF)
  175. assert(S.size() > COFF::NameSize && "Short string in COFF string table!");
  176. assert(!isFinalized());
  177. auto P = StringIndexMap.insert(std::make_pair(S, 0));
  178. if (P.second) {
  179. size_t Start = alignTo(Size, Alignment);
  180. P.first->second = Start;
  181. Size = Start + S.size() + (K != RAW);
  182. }
  183. return P.first->second;
  184. }