Skip to content

Commit b6c5a30

Browse files
committedMay 10, 2018
COFF: Allow ICF on vtable sections.
Differential Revision: https://reviews.llvm.org/D46734 llvm-svn: 332059
1 parent 0c2f6be commit b6c5a30

File tree

2 files changed

+38
-5
lines changed

2 files changed

+38
-5
lines changed
 

‎lld/COFF/ICF.cpp

+10-5
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,10 @@ uint32_t ICF::getHash(SectionChunk *C) {
7777
// 2017) says that /opt:icf folds both functions and read-only data.
7878
// Despite that, the MSVC linker folds only functions. We found
7979
// a few instances of programs that are not safe for data merging.
80-
// Therefore, we merge only functions just like the MSVC tool. However, we merge
81-
// identical .xdata sections, because the address of unwind information is
82-
// insignificant to the user program and the Visual C++ linker does this.
80+
// Therefore, we merge only functions just like the MSVC tool. However, we also
81+
// merge read-only sections in a couple of cases where the address of the
82+
// section is insignificant to the user program and the behaviour matches that
83+
// of the Visual C++ linker.
8384
bool ICF::isEligible(SectionChunk *C) {
8485
// Non-comdat chunks, dead chunks, and writable chunks are not elegible.
8586
bool Writable = C->getOutputCharacteristics() & llvm::COFF::IMAGE_SCN_MEM_WRITE;
@@ -90,8 +91,12 @@ bool ICF::isEligible(SectionChunk *C) {
9091
if (C->getOutputCharacteristics() & llvm::COFF::IMAGE_SCN_MEM_EXECUTE)
9192
return true;
9293

93-
// .xdata unwind info sections are eligble.
94-
return C->getSectionName().split('$').first == ".xdata";
94+
// .xdata unwind info sections are eligible.
95+
if (C->getSectionName().split('$').first == ".xdata")
96+
return true;
97+
98+
// So are vtables.
99+
return C->Sym && C->Sym->getName().startswith("??_7");
95100
}
96101

97102
// Split an equivalence class into smaller classes.

‎lld/test/COFF/icf-vtables.s

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# REQUIRES: x86
2+
# RUN: llvm-mc -triple=x86_64-windows-msvc -filetype=obj -o %t.obj %s
3+
# RUN: lld-link %t.obj /out:%t.exe /entry:main /subsystem:console
4+
# RUN: llvm-objdump -s %t.exe | FileCheck %s
5+
6+
# CHECK: Contents of section .text:
7+
.globl main
8+
main:
9+
# CHECK-NEXT: 140001000 00200040 01000000 01200040 01000000
10+
.8byte "??_"
11+
.8byte "??_7"
12+
# CHECK-NEXT: 140001010 01200040 01000000
13+
.8byte "??_7a"
14+
15+
.section .rdata,"dr",discard,"??_"
16+
.globl "??_"
17+
"??_":
18+
.byte 42
19+
20+
.section .rdata,"dr",discard,"??_7"
21+
.globl "??_7"
22+
"??_7":
23+
.byte 42
24+
25+
.section .rdata,"dr",discard,"??_7a"
26+
.globl "??_7a"
27+
"??_7a":
28+
.byte 42

0 commit comments

Comments
 (0)