Bug 22483 – DMD generates invalid string sections that work by coincidence
Status
RESOLVED
Resolution
FIXED
Severity
minor
Priority
P3
Component
dmd
Product
D
Version
D2
Platform
x86_64
OS
Linux
Creation time
2021-11-05T06:18:23Z
Last change time
2024-01-01T22:05:07Z
Keywords
backend, pull, wrong-code
Assigned to
No Owner
Creator
FeepingCreature
Comments
Comment #0 by default_357-line — 2021-11-05T06:18:23Z
See https://github.com/rui314/mold/issues/126
DMD generates .rodata.str1.1 string sections (for char[] strings) with a sh_entsize of 0, when it should be 1. Linkers apparently react to this by not treating the section as mergeable at all.
You can confirm this:
```
echo 'string foo() { return "Hello World"; }' > a.d
echo 'string bar() { return "Hello World"; }' > b.d
echo 'import a, b; void main() { assert(foo.ptr is bar.ptr); }' > c.d
# force creating separate string sections
dmd a.d -c -ofa.o
dmd b.d -c -ofb.o
dmd a.o b.o c.d -ofstrsectiontest
# will fail
./strsectiontest
```
Patch:
```
diff --git a/src/dmd/backend/elfobj.d b/src/dmd/backend/elfobj.d
index b555b01e0..02e888e8d 100644
--- a/src/dmd/backend/elfobj.d
+++ b/src/dmd/backend/elfobj.d
@@ -630,6 +630,7 @@ int ElfObj_string_literal_segment(uint sz)
const int i = (sz == 4) ? 2 : sz - 1;
const IDXSEC seg =
ElfObj_getsegment(".rodata.str".ptr, name[i].ptr, SHT_PROGBITS, SHF_ALLOC | SHF_MERGE | SHF_STRINGS, sz);
+ MAP_SEG2SEC(seg).sh_entsize = sz;
return seg;
}
```
But with this applied, DMD generates invalid revocations:
/usr/bin/ld: a.o: access beyond end of merged section (-4)
/usr/bin/ld: b.o: access beyond end of merged section (-4)
/usr/bin/ld: strsectiontest.o: access beyond end of merged section (-4)
Afaics, the revocations were always invalid, but section merging didn't happen for the .rodata.str, so we didn't notice. How to fix that is beyond me though.
Comment #1 by default_357-line — 2021-11-05T06:21:01Z
relocations*
Comment #2 by dlang-bot — 2023-12-16T02:47:36Z
@ssvb created dlang/dmd pull request #15915 "fix Issue 22483 - DMD generates invalid string sections that work by coincidence" fixing this issue:
- fix Issue 22483 - DMD generates invalid string sections that work by coincidence
DMD generates bogus ".rodata.str" segment, which isn't really mergeable
by proper linkers and this trips https://github.com/rui314/mold linker
up. As a workaround, just don't set the SHF_MERGE | SHF_STRINGS flags.
https://github.com/dlang/dmd/pull/15915
Comment #3 by dlang-bot — 2023-12-20T13:12:02Z
dlang/dmd pull request #15915 "fix Issue 22483 - DMD generates invalid string sections that work by coincidence" was merged into stable:
- 4542057926be4f456d6411059231a2ede7ae33b6 by Siarhei Siamashka:
fix Issue 22483 - DMD generates invalid string sections that work by coincidence
DMD generates bogus ".rodata.str" segment, which isn't really mergeable
by proper linkers and this trips https://github.com/rui314/mold linker
up. As a workaround, just don't set the SHF_MERGE | SHF_STRINGS flags.
https://github.com/dlang/dmd/pull/15915
Comment #4 by dlang-bot — 2024-01-01T22:05:07Z
dlang/dmd pull request #15978 "merge stable" was merged into master:
- 3a85e6bdbee15235d12f714f608a757ad1983bc3 by Siarhei Siamashka:
fix Issue 22483 - DMD generates invalid string sections that work by coincidence (#15915)
DMD generates bogus ".rodata.str" segment, which isn't really mergeable
by proper linkers and this trips https://github.com/rui314/mold linker
up. As a workaround, just don't set the SHF_MERGE | SHF_STRINGS flags.
https://github.com/dlang/dmd/pull/15978