Skip to content

Commit 4204ca0

Browse files
committed
[lld][COFF] Remove duplicate strtab entries
String table size is too big for large binary when symbol table is enabled. Some strings in strtab is same so it can be reused.
1 parent 34e63be commit 4204ca0

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

lld/COFF/Writer.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,7 @@ class Writer {
285285
std::unique_ptr<FileOutputBuffer> &buffer;
286286
std::map<PartialSectionKey, PartialSection *> partialSections;
287287
std::vector<char> strtab;
288+
StringMap<size_t> strtabMap;
288289
std::vector<llvm::object::coff_symbol16> outputSymtab;
289290
std::vector<ECCodeMapEntry> codeMap;
290291
IdataContents idata;
@@ -1439,10 +1440,13 @@ void Writer::assignOutputSectionIndices() {
14391440

14401441
size_t Writer::addEntryToStringTable(StringRef str) {
14411442
assert(str.size() > COFF::NameSize);
1442-
size_t offsetOfEntry = strtab.size() + 4; // +4 for the size field
1443-
strtab.insert(strtab.end(), str.begin(), str.end());
1444-
strtab.push_back('\0');
1445-
return offsetOfEntry;
1443+
size_t newOffsetOfEntry = strtab.size() + 4; // +4 for the size field
1444+
auto res = strtabMap.try_emplace(str, newOffsetOfEntry);
1445+
if (res.second) {
1446+
strtab.insert(strtab.end(), str.begin(), str.end());
1447+
strtab.push_back('\0');
1448+
}
1449+
return res.first->getValue();
14461450
}
14471451

14481452
std::optional<coff_symbol16> Writer::createSymbol(Defined *def) {

0 commit comments

Comments
 (0)