Skip to content

Commit c676104

Browse files
authored
[lld-macho] Implement symbol string deduplication (#123874)
The symbol string table does not have deduplication. Here we add code to deduplicate the symbol string table. This has a rather large size impact (20-30%) on unstripped binaries (typically debug binaries) but no size impact on stripped binaries(typically release binaries). We enable deduplication by default and add a flag to disable it (`-no-deduplicate-symbol-strings`).
1 parent 28ad897 commit c676104

File tree

6 files changed

+27
-1
lines changed

6 files changed

+27
-1
lines changed

lld/MachO/Config.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ struct Configuration {
143143
bool timeTraceEnabled = false;
144144
bool dataConst = false;
145145
bool dedupStrings = true;
146+
bool dedupSymbolStrings = true;
146147
bool deadStripDuplicates = false;
147148
bool omitDebugInfo = false;
148149
bool warnDylibInstallName = false;

lld/MachO/Driver.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1806,6 +1806,7 @@ bool link(ArrayRef<const char *> argsArr, llvm::raw_ostream &stdoutOS,
18061806
config->keepICFStabs = args.hasArg(OPT_keep_icf_stabs);
18071807
config->dedupStrings =
18081808
args.hasFlag(OPT_deduplicate_strings, OPT_no_deduplicate_strings, true);
1809+
config->dedupSymbolStrings = !args.hasArg(OPT_no_deduplicate_symbol_strings);
18091810
config->deadStripDuplicates = args.hasArg(OPT_dead_strip_duplicates);
18101811
config->warnDylibInstallName = args.hasFlag(
18111812
OPT_warn_dylib_install_name, OPT_no_warn_dylib_install_name, false);

lld/MachO/Options.td

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1476,3 +1476,8 @@ def no_warn_duplicate_libraries : Flag<["-"], "no_warn_duplicate_libraries">,
14761476
HelpText<"Do not warn if the input contains duplicate library options.">,
14771477
Flags<[HelpHidden]>,
14781478
Group<grp_ignored_silently>;
1479+
1480+
// Add this with the other flags in the rare options group
1481+
def no_deduplicate_symbol_strings : Flag<["-"], "no-deduplicate-symbol-strings">,
1482+
HelpText<"Do not deduplicate strings in the symbol string table. Might result in larger binaries but slightly faster link times.">,
1483+
Group<grp_rare>;

lld/MachO/SyntheticSections.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1541,7 +1541,14 @@ StringTableSection::StringTableSection()
15411541

15421542
uint32_t StringTableSection::addString(StringRef str) {
15431543
uint32_t strx = size;
1544-
strings.push_back(str); // TODO: consider deduplicating strings
1544+
if (config->dedupSymbolStrings) {
1545+
llvm::CachedHashStringRef hashedStr(str);
1546+
auto [it, inserted] = stringMap.try_emplace(hashedStr, strx);
1547+
if (!inserted)
1548+
return it->second;
1549+
}
1550+
1551+
strings.push_back(str);
15451552
size += str.size() + 1; // account for null terminator
15461553
return strx;
15471554
}

lld/MachO/SyntheticSections.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,7 @@ class StringTableSection final : public LinkEditSection {
447447
// match its behavior here since some tools depend on it.
448448
// Consequently, the empty string will be at index 1, not zero.
449449
std::vector<StringRef> strings{" "};
450+
llvm::DenseMap<llvm::CachedHashStringRef, uint32_t> stringMap;
450451
size_t size = 2;
451452
};
452453

lld/test/MachO/cfstring-dedup.s

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,17 @@
77
# RUN: %lld -dylib -framework CoreFoundation %t/foo1.o %t/foo2.o -o %t/foo
88
# RUN: llvm-objdump --no-print-imm-hex --macho --rebase --bind --syms -d %t/foo | FileCheck %s --check-prefix=LITERALS
99

10+
# Check that string deduplication for symbol names is working
11+
# RUN: %lld -dylib -framework CoreFoundation %t/foo1.o %t/foo2.o -o %t/foo_no_dedup -no-deduplicate-symbol-strings
12+
# RUN: llvm-strings %t/foo | FileCheck %s --check-prefix=CHECK-DEDUP
13+
# RUN: llvm-strings %t/foo_no_dedup | FileCheck %s --check-prefix=CHECK-NO-DEDUP
14+
# CHECK-DEDUP: _named_cfstring
15+
# CHECK-DEDUP-NOT: _named_cfstring
16+
# CHECK-NO-DEDUP: _named_cfstring
17+
# CHECK-NO-DEDUP: _named_cfstring
18+
# CHECK-NO-DEDUP-NOT: _named_cfstring
19+
20+
1021
# CHECK: (__TEXT,__text) section
1122
# CHECK-NEXT: _foo1:
1223
# CHECK-NEXT: _foo2:

0 commit comments

Comments
 (0)