[lld] c541bbf - [LLD][COFF] Prevent merging .bss into shared sections (#202817)
via llvm-commits
llvm-commits at lists.llvm.org
Wed Jul 1 03:59:22 PDT 2026
Author: Evgenii Kudriashov
Date: 2026-07-01T12:59:18+02:00
New Revision: c541bbfa346d3da09154f3268079dca841ee9d72
URL: https://github.com/llvm/llvm-project/commit/c541bbfa346d3da09154f3268079dca841ee9d72
DIFF: https://github.com/llvm/llvm-project/commit/c541bbfa346d3da09154f3268079dca841ee9d72.diff
LOG: [LLD][COFF] Prevent merging .bss into shared sections (#202817)
This matches the behavior with MSVC linker. Generally the idea is that
.bss section may contain security cookies that shouldn't be shared among
processes.
Added:
lld/test/COFF/bss-shared-no-merge.test
Modified:
lld/COFF/Writer.cpp
Removed:
################################################################################
diff --git a/lld/COFF/Writer.cpp b/lld/COFF/Writer.cpp
index 955a9897ac2b0..33826c9146c50 100644
--- a/lld/COFF/Writer.cpp
+++ b/lld/COFF/Writer.cpp
@@ -216,6 +216,7 @@ class Writer {
void appendImportThunks();
void locateImportTables();
void createExportTable();
+ StringRef getMergeDestination(StringRef fromSection, StringRef toSection);
void mergeSection(const std::map<StringRef, StringRef>::value_type &p);
void mergeSections();
void sortECChunks();
@@ -1656,25 +1657,32 @@ void Writer::createSymbolAndStringTable() {
fileSize = alignTo(fileOff, ctx.config.fileAlign);
}
-void Writer::mergeSection(const std::map<StringRef, StringRef>::value_type &p) {
- StringRef toName = p.second;
- if (p.first == toName)
- return;
+StringRef Writer::getMergeDestination(StringRef fromSection,
+ StringRef toSection) {
StringSet<> names;
while (true) {
- if (!names.insert(toName).second)
- Fatal(ctx) << "/merge: cycle found for section '" << p.first << "'";
- auto i = ctx.config.merge.find(toName);
+ if (!names.insert(toSection).second)
+ Fatal(ctx) << "/merge: cycle found for section '" << fromSection << "'";
+ auto i = ctx.config.merge.find(toSection);
if (i == ctx.config.merge.end())
break;
- toName = i->second;
+ toSection = i->second;
}
+ return toSection;
+}
+
+void Writer::mergeSection(const std::map<StringRef, StringRef>::value_type &p) {
+ if (p.first == p.second)
+ return;
+
+ StringRef toSection = getMergeDestination(p.first, p.second);
+
OutputSection *from = findSection(p.first);
- OutputSection *to = findSection(toName);
+ OutputSection *to = findSection(toSection);
if (!from)
return;
if (!to) {
- from->name = toName;
+ from->name = toSection;
return;
}
to->merge(from);
@@ -1716,8 +1724,17 @@ void Writer::mergeSections() {
// whatever section it is being merged into (usually .data) so that the image
// need not actually contain all of the zeros.
auto it = ctx.config.merge.find(".bss");
- if (it != ctx.config.merge.end())
- mergeSection(*it);
+ if (it != ctx.config.merge.end()) {
+ // Resolve the final merge target name following the chain.
+ StringRef toSection = getMergeDestination(it->first, it->second);
+ // Don't merge .bss into a shared section. MSVC link.exe keeps .bss
+ // separate when the target has IMAGE_SCN_MEM_SHARED, preventing unexpected
+ // sharing across processes.
+ auto secIt = ctx.config.section.find(toSection);
+ if (secIt == ctx.config.section.end() ||
+ !(secIt->second & IMAGE_SCN_MEM_SHARED))
+ mergeSection({it->first, toSection});
+ }
}
// EC targets may have chunks of various architectures mixed together at this
diff --git a/lld/test/COFF/bss-shared-no-merge.test b/lld/test/COFF/bss-shared-no-merge.test
new file mode 100644
index 0000000000000..497ed0c85ce05
--- /dev/null
+++ b/lld/test/COFF/bss-shared-no-merge.test
@@ -0,0 +1,85 @@
+# REQUIRES: x86
+# Test that .bss is not merged into .data when .data is marked shared.
+# MSVC link.exe keeps .bss separate to prevent unexpected sharing across
+# processes.
+
+# RUN: yaml2obj %s -o %t.obj
+
+## .bss should NOT be merged when .data is shared.
+# RUN: lld-link /out:%t.dll /dll /entry:DllMain /section:.data,rws %t.obj
+# RUN: llvm-readobj --sections %t.dll | FileCheck -check-prefix=SHARED %s
+
+# SHARED: Name: .bss
+# SHARED: Name: .data
+
+## .bss should still be merged when .data is not shared (default behavior).
+# RUN: lld-link /out:%t2.dll /dll /entry:DllMain %t.obj
+# RUN: llvm-readobj --sections %t2.dll | FileCheck -check-prefix=NOSHARED %s
+
+# NOSHARED-NOT: Name: .bss
+# NOSHARED: Name: .data
+
+--- !COFF
+header:
+ Machine: IMAGE_FILE_MACHINE_AMD64
+ Characteristics: [ ]
+sections:
+ - Name: .text
+ Characteristics: [ IMAGE_SCN_CNT_CODE, IMAGE_SCN_MEM_EXECUTE, IMAGE_SCN_MEM_READ ]
+ Alignment: 4
+ SectionData: 'C3'
+ SizeOfRawData: 1
+ - Name: .data
+ Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_READ, IMAGE_SCN_MEM_WRITE ]
+ Alignment: 4
+ SectionData: '01000000'
+ SizeOfRawData: 4
+ - Name: .bss
+ Characteristics: [ IMAGE_SCN_CNT_UNINITIALIZED_DATA, IMAGE_SCN_MEM_READ, IMAGE_SCN_MEM_WRITE ]
+ Alignment: 4
+ SectionData: ''
+ SizeOfRawData: 256
+symbols:
+ - Name: .text
+ Value: 0
+ SectionNumber: 1
+ SimpleType: IMAGE_SYM_TYPE_NULL
+ ComplexType: IMAGE_SYM_DTYPE_NULL
+ StorageClass: IMAGE_SYM_CLASS_STATIC
+ SectionDefinition:
+ Length: 1
+ NumberOfRelocations: 0
+ NumberOfLinenumbers: 0
+ CheckSum: 0
+ Number: 1
+ - Name: .data
+ Value: 0
+ SectionNumber: 2
+ SimpleType: IMAGE_SYM_TYPE_NULL
+ ComplexType: IMAGE_SYM_DTYPE_NULL
+ StorageClass: IMAGE_SYM_CLASS_STATIC
+ SectionDefinition:
+ Length: 4
+ NumberOfRelocations: 0
+ NumberOfLinenumbers: 0
+ CheckSum: 0
+ Number: 2
+ - Name: .bss
+ Value: 0
+ SectionNumber: 3
+ SimpleType: IMAGE_SYM_TYPE_NULL
+ ComplexType: IMAGE_SYM_DTYPE_NULL
+ StorageClass: IMAGE_SYM_CLASS_STATIC
+ SectionDefinition:
+ Length: 256
+ NumberOfRelocations: 0
+ NumberOfLinenumbers: 0
+ CheckSum: 0
+ Number: 3
+ - Name: DllMain
+ Value: 0
+ SectionNumber: 1
+ SimpleType: IMAGE_SYM_TYPE_NULL
+ ComplexType: IMAGE_SYM_DTYPE_FUNCTION
+ StorageClass: IMAGE_SYM_CLASS_EXTERNAL
+...
More information about the llvm-commits
mailing list