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/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"
26 StringTableBuilder::~StringTableBuilder() = default;
28 void StringTableBuilder::initSize() {
29 // Account for leading bytes in table so that offsets returned from add are
38 // Start the table with a NUL byte.
43 // Make room to write the table size later.
49 StringTableBuilder::StringTableBuilder(Kind K
, unsigned Alignment
)
50 : K(K
), Alignment(Alignment
) {
54 void StringTableBuilder::write(raw_ostream
&OS
) const {
55 assert(isFinalized());
57 Data
.resize(getSize());
58 write((uint8_t *)Data
.data());
62 using StringPair
= std::pair
<CachedHashStringRef
, size_t>;
64 void StringTableBuilder::write(uint8_t *Buf
) const {
65 assert(isFinalized());
66 for (const StringPair
&P
: StringIndexMap
) {
67 StringRef Data
= P
.first
.val();
69 memcpy(Buf
+ P
.second
, Data
.data(), Data
.size());
71 // The COFF formats store the size of the string table in the first 4 bytes.
72 // For Windows, the format is little-endian; for AIX, it is big-endian.
74 support::endian::write32le(Buf
, Size
);
76 support::endian::write32be(Buf
, Size
);
79 // Returns the character at Pos from end of a string.
80 static int charTailAt(StringPair
*P
, size_t Pos
) {
81 StringRef S
= P
->first
.val();
84 return (unsigned char)S
[S
.size() - Pos
- 1];
87 // Three-way radix quicksort. This is much faster than std::sort with strcmp
88 // because it does not compare characters that we already know the same.
89 static void multikeySort(MutableArrayRef
<StringPair
*> Vec
, int Pos
) {
94 // Partition items so that items in [0, I) are greater than the pivot,
95 // [I, J) are the same as the pivot, and [J, Vec.size()) are less than
97 int Pivot
= charTailAt(Vec
[0], Pos
);
99 size_t J
= Vec
.size();
100 for (size_t K
= 1; K
< J
;) {
101 int C
= charTailAt(Vec
[K
], Pos
);
103 std::swap(Vec
[I
++], Vec
[K
++]);
105 std::swap(Vec
[--J
], Vec
[K
]);
110 multikeySort(Vec
.slice(0, I
), Pos
);
111 multikeySort(Vec
.slice(J
), Pos
);
113 // multikeySort(Vec.slice(I, J - I), Pos + 1), but with
114 // tail call optimization.
116 Vec
= Vec
.slice(I
, J
- I
);
122 void StringTableBuilder::finalize() {
124 finalizeStringTable(/*Optimize=*/true);
127 void StringTableBuilder::finalizeInOrder() {
128 finalizeStringTable(/*Optimize=*/false);
131 void StringTableBuilder::finalizeStringTable(bool Optimize
) {
135 std::vector
<StringPair
*> Strings
;
136 Strings
.reserve(StringIndexMap
.size());
137 for (StringPair
&P
: StringIndexMap
)
138 Strings
.push_back(&P
);
140 multikeySort(Strings
, 0);
144 for (StringPair
*P
: Strings
) {
145 StringRef S
= P
->first
.val();
146 if (Previous
.endswith(S
)) {
147 size_t Pos
= Size
- S
.size() - (K
!= RAW
);
148 if (!(Pos
& (Alignment
- 1))) {
154 Size
= alignTo(Size
, Alignment
);
165 Size
= alignTo(Size
, 4); // Pad to multiple of 4.
167 // The first byte in an ELF string table must be null, according to the ELF
168 // specification. In 'initSize()' we reserved the first byte to hold null for
169 // this purpose and here we actually add the string to allow 'getOffset()' to
170 // be called on an empty string.
172 StringIndexMap
[CachedHashStringRef("")] = 0;
175 void StringTableBuilder::clear() {
177 StringIndexMap
.clear();
180 size_t StringTableBuilder::getOffset(CachedHashStringRef S
) const {
181 assert(isFinalized());
182 auto I
= StringIndexMap
.find(S
);
183 assert(I
!= StringIndexMap
.end() && "String is not in table!");
187 size_t StringTableBuilder::add(CachedHashStringRef S
) {
189 assert(S
.size() > COFF::NameSize
&& "Short string in COFF string table!");
191 assert(!isFinalized());
192 auto P
= StringIndexMap
.insert(std::make_pair(S
, 0));
194 size_t Start
= alignTo(Size
, Alignment
);
195 P
.first
->second
= Start
;
196 Size
= Start
+ S
.size() + (K
!= RAW
);
198 return P
.first
->second
;