Skip to content

Commit

Permalink
Merge pull request ldc-developers#4748 from kinke/fix3589
Browse files Browse the repository at this point in the history
ELF: Emit (most) instantiated symbols in COMDATs
  • Loading branch information
kinke authored Sep 9, 2024
2 parents 580ff68 + 43f59b1 commit 58280f5
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#### Bug fixes
- Fix potentially corrupt IR layouts for bit fields. (#4646, #4708)
- Fix potentially corrupt IR layouts for explicitly under-aligned aggregates, a regression introduced in LDC v1.31. (#4734, #4736)
- ELF: Emit (most) instantiated symbols in COMDATs for proper link-time culling. (#3589, #4748)

# LDC 1.39.0 (2024-07-04)

Expand Down
8 changes: 7 additions & 1 deletion gen/tollvm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,13 @@ LLGlobalValue::LinkageTypes DtoLinkageOnly(Dsymbol *sym) {
}

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

bool needsCOMDAT() {
Expand Down
22 changes: 22 additions & 0 deletions tests/codegen/gh3589.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// UNSUPPORTED: Windows || Darwin

// emit duplicate instantiated globals into two object files:
// RUN: %ldc -c %S/inputs/gh3589_a.d -I%S/inputs -of=%t_a.o
// RUN: %ldc -c %S/inputs/gh3589_b.d -I%S/inputs -of=%t_b.o

// link & run:
// RUN: %ldc -I%S/inputs %t_a.o %t_b.o -run %s

extern extern(C) __gshared {
// magic linker symbols to refer to the start and end of test_section
byte __start_test_section;
byte __stop_test_section;
}

void main() {
import gh3589_a, gh3589_b;
assert(a_info == b_info);

const sectionSize = cast(size_t) (&__stop_test_section - &__start_test_section);
assert(sectionSize == 4);
}
2 changes: 2 additions & 0 deletions tests/codegen/inputs/gh3589_a.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import gh3589_templ;
shared a_info = &getInfo!0;
2 changes: 2 additions & 0 deletions tests/codegen/inputs/gh3589_b.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import gh3589_templ;
shared b_info = &getInfo!0;
4 changes: 4 additions & 0 deletions tests/codegen/inputs/gh3589_templ.di
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import ldc.attributes;
template getInfo(int I) {
@(section("test_section")) @assumeUsed shared int getInfo = I;
}

0 comments on commit 58280f5

Please sign in to comment.