[lld] [lld][COFF] Remove duplicate strtab entries (PR #141197)
Haohai Wen via llvm-commits
llvm-commits at lists.llvm.org
Thu May 22 21:42:22 PDT 2025
https://github.com/HaohaiWen created https://github.com/llvm/llvm-project/pull/141197
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.
>From 4204ca07e85470fa9169025f1c7a8172f4bb03c9 Mon Sep 17 00:00:00 2001
From: Haohai Wen <haohai.wen at intel.com>
Date: Fri, 23 May 2025 12:36:07 +0800
Subject: [PATCH] [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.
---
lld/COFF/Writer.cpp | 12 ++++++++----
1 file changed, 8 insertions(+), 4 deletions(-)
diff --git a/lld/COFF/Writer.cpp b/lld/COFF/Writer.cpp
index db6133e20a037..6ec83a5f77e5a 100644
--- a/lld/COFF/Writer.cpp
+++ b/lld/COFF/Writer.cpp
@@ -285,6 +285,7 @@ class Writer {
std::unique_ptr<FileOutputBuffer> &buffer;
std::map<PartialSectionKey, PartialSection *> partialSections;
std::vector<char> strtab;
+ StringMap<size_t> strtabMap;
std::vector<llvm::object::coff_symbol16> outputSymtab;
std::vector<ECCodeMapEntry> codeMap;
IdataContents idata;
@@ -1439,10 +1440,13 @@ void Writer::assignOutputSectionIndices() {
size_t Writer::addEntryToStringTable(StringRef str) {
assert(str.size() > COFF::NameSize);
- size_t offsetOfEntry = strtab.size() + 4; // +4 for the size field
- strtab.insert(strtab.end(), str.begin(), str.end());
- strtab.push_back('\0');
- return offsetOfEntry;
+ size_t newOffsetOfEntry = strtab.size() + 4; // +4 for the size field
+ auto res = strtabMap.try_emplace(str, newOffsetOfEntry);
+ if (res.second) {
+ strtab.insert(strtab.end(), str.begin(), str.end());
+ strtab.push_back('\0');
+ }
+ return res.first->getValue();
}
std::optional<coff_symbol16> Writer::createSymbol(Defined *def) {
More information about the llvm-commits
mailing list