1 //===- StringTableBuilder.cpp - String table building utility -------------===//
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
7 //===----------------------------------------------------------------------===//
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/BinaryFormat/COFF.h"
15 #include "llvm/Support/Endian.h"
16 #include "llvm/Support/MathExtras.h"
17 #include "llvm/Support/raw_ostream.h"
27 StringTableBuilder::~StringTableBuilder() = default;
29 void StringTableBuilder::initSize() {
30 // Account for leading bytes in table so that offsets returned from add are
45 // Start the table with a NUL byte.
50 // Make room to write the table size later.
56 StringTableBuilder::StringTableBuilder(Kind K
, Align Alignment
)
57 : K(K
), Alignment(Alignment
) {
61 void StringTableBuilder::write(raw_ostream
&OS
) const {
62 assert(isFinalized());
64 Data
.resize(getSize());
65 write((uint8_t *)Data
.data());
69 using StringPair
= std::pair
<CachedHashStringRef
, size_t>;
71 void StringTableBuilder::write(uint8_t *Buf
) const {
72 assert(isFinalized());
73 for (const StringPair
&P
: StringIndexMap
) {
74 StringRef Data
= P
.first
.val();
76 memcpy(Buf
+ P
.second
, Data
.data(), Data
.size());
78 // The COFF formats store the size of the string table in the first 4 bytes.
79 // For Windows, the format is little-endian; for AIX, it is big-endian.
81 support::endian::write32le(Buf
, Size
);
83 support::endian::write32be(Buf
, Size
);
86 // Returns the character at Pos from end of a string.
87 static int charTailAt(StringPair
*P
, size_t Pos
) {
88 StringRef S
= P
->first
.val();
91 return (unsigned char)S
[S
.size() - Pos
- 1];
94 // Three-way radix quicksort. This is much faster than std::sort with strcmp
95 // because it does not compare characters that we already know the same.
96 static void multikeySort(MutableArrayRef
<StringPair
*> Vec
, int Pos
) {
101 // Partition items so that items in [0, I) are greater than the pivot,
102 // [I, J) are the same as the pivot, and [J, Vec.size()) are less than
104 int Pivot
= charTailAt(Vec
[0], Pos
);
106 size_t J
= Vec
.size();
107 for (size_t K
= 1; K
< J
;) {
108 int C
= charTailAt(Vec
[K
], Pos
);
110 std::swap(Vec
[I
++], Vec
[K
++]);
112 std::swap(Vec
[--J
], Vec
[K
]);
117 multikeySort(Vec
.slice(0, I
), Pos
);
118 multikeySort(Vec
.slice(J
), Pos
);
120 // multikeySort(Vec.slice(I, J - I), Pos + 1), but with
121 // tail call optimization.
123 Vec
= Vec
.slice(I
, J
- I
);
129 void StringTableBuilder::finalize() {
131 finalizeStringTable(/*Optimize=*/true);
134 void StringTableBuilder::finalizeInOrder() {
135 finalizeStringTable(/*Optimize=*/false);
138 void StringTableBuilder::finalizeStringTable(bool Optimize
) {
142 std::vector
<StringPair
*> Strings
;
143 Strings
.reserve(StringIndexMap
.size());
144 for (StringPair
&P
: StringIndexMap
)
145 Strings
.push_back(&P
);
147 multikeySort(Strings
, 0);
151 for (StringPair
*P
: Strings
) {
152 StringRef S
= P
->first
.val();
153 if (Previous
.ends_with(S
)) {
154 size_t Pos
= Size
- S
.size() - (K
!= RAW
);
155 if (isAligned(Alignment
, Pos
)) {
161 Size
= alignTo(Size
, Alignment
);
171 if (K
== MachO
|| K
== MachOLinked
|| K
== DXContainer
)
172 Size
= alignTo(Size
, 4); // Pad to multiple of 4.
173 if (K
== MachO64
|| K
== MachO64Linked
)
174 Size
= alignTo(Size
, 8); // Pad to multiple of 8.
176 // According to ld64 the string table of a final linked Mach-O binary starts
177 // with " ", i.e. the first byte is ' ' and the second byte is zero. In
178 // 'initSize()' we reserved the first two bytes for holding this string.
179 if (K
== MachOLinked
|| K
== MachO64Linked
)
180 StringIndexMap
[CachedHashStringRef(" ")] = 0;
182 // The first byte in an ELF string table must be null, according to the ELF
183 // specification. In 'initSize()' we reserved the first byte to hold null for
184 // this purpose and here we actually add the string to allow 'getOffset()' to
185 // be called on an empty string.
187 StringIndexMap
[CachedHashStringRef("")] = 0;
190 void StringTableBuilder::clear() {
192 StringIndexMap
.clear();
195 size_t StringTableBuilder::getOffset(CachedHashStringRef S
) const {
196 assert(isFinalized());
197 auto I
= StringIndexMap
.find(S
);
198 assert(I
!= StringIndexMap
.end() && "String is not in table!");
202 size_t StringTableBuilder::add(CachedHashStringRef S
) {
204 assert(S
.size() > COFF::NameSize
&& "Short string in COFF string table!");
206 assert(!isFinalized());
207 auto P
= StringIndexMap
.insert(std::make_pair(S
, 0));
209 size_t Start
= alignTo(Size
, Alignment
);
210 P
.first
->second
= Start
;
211 Size
= Start
+ S
.size() + (K
!= RAW
);
213 return P
.first
->second
;