Skip to content

Commit 58280f5

Browse files
authored
Merge pull request #4748 from kinke/fix3589
ELF: Emit (most) instantiated symbols in COMDATs
2 parents 580ff68 + 43f59b1 commit 58280f5

File tree

6 files changed

+38
-1
lines changed

6 files changed

+38
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#### Bug fixes
1313
- Fix potentially corrupt IR layouts for bit fields. (#4646, #4708)
1414
- Fix potentially corrupt IR layouts for explicitly under-aligned aggregates, a regression introduced in LDC v1.31. (#4734, #4736)
15+
- ELF: Emit (most) instantiated symbols in COMDATs for proper link-time culling. (#3589, #4748)
1516

1617
# LDC 1.39.0 (2024-07-04)
1718

gen/tollvm.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,13 @@ LLGlobalValue::LinkageTypes DtoLinkageOnly(Dsymbol *sym) {
259259
}
260260

261261
LinkageWithCOMDAT DtoLinkage(Dsymbol *sym) {
262-
return {DtoLinkageOnly(sym), needsCOMDAT()};
262+
const auto linkage = DtoLinkageOnly(sym);
263+
const bool inCOMDAT = needsCOMDAT() ||
264+
// ELF needs some help for ODR linkages:
265+
// https://github.com/ldc-developers/ldc/issues/3589
266+
(linkage == templateLinkage &&
267+
global.params.targetTriple->isOSBinFormatELF());
268+
return {linkage, inCOMDAT};
263269
}
264270

265271
bool needsCOMDAT() {

tests/codegen/gh3589.d

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// UNSUPPORTED: Windows || Darwin
2+
3+
// emit duplicate instantiated globals into two object files:
4+
// RUN: %ldc -c %S/inputs/gh3589_a.d -I%S/inputs -of=%t_a.o
5+
// RUN: %ldc -c %S/inputs/gh3589_b.d -I%S/inputs -of=%t_b.o
6+
7+
// link & run:
8+
// RUN: %ldc -I%S/inputs %t_a.o %t_b.o -run %s
9+
10+
extern extern(C) __gshared {
11+
// magic linker symbols to refer to the start and end of test_section
12+
byte __start_test_section;
13+
byte __stop_test_section;
14+
}
15+
16+
void main() {
17+
import gh3589_a, gh3589_b;
18+
assert(a_info == b_info);
19+
20+
const sectionSize = cast(size_t) (&__stop_test_section - &__start_test_section);
21+
assert(sectionSize == 4);
22+
}

tests/codegen/inputs/gh3589_a.d

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import gh3589_templ;
2+
shared a_info = &getInfo!0;

tests/codegen/inputs/gh3589_b.d

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import gh3589_templ;
2+
shared b_info = &getInfo!0;

tests/codegen/inputs/gh3589_templ.di

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import ldc.attributes;
2+
template getInfo(int I) {
3+
@(section("test_section")) @assumeUsed shared int getInfo = I;
4+
}

0 commit comments

Comments
 (0)