StringTableBuilder.cpp 5.8 KB

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