Bug 21120 – Inconsistent mangling of __init symbol
Status
RESOLVED
Resolution
FIXED
Severity
major
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2020-08-05T11:22:22Z
Last change time
2020-09-11T06:12:23Z
Keywords
industry, pull
Assigned to
No Owner
Creator
johanengelen
Comments
Comment #0 by johanengelen — 2020-08-05T11:22:22Z
Testcase:
File three.d:
```
module one.two.three;
StructTemplate!B instatiation;
struct B
{
}
struct StructTemplate(Opts)
{
void foo()
{
someTemplate!StructTemplate();
}
}
template someTemplate(T) {
pragma(msg, "_D" ~ T.mangleof[1..$] ~ "6__initZ");
pragma(mangle, "_D" ~ T.mangleof[1..$] ~ "6__initZ")
extern immutable typeof(T.init) initializer;
void someTemplate()
{
auto ptr = &initializer;
}
}
```
> dmd -of=/tmp/wrongmangle.o -c three.d
output: _D3one3two5three__T14StructTemplateTSQBjQBiQBh1BZQBe6__initZ
> nm /tmp/wrongmangle.o | grep ".*StructTemplate.*__initZ"
outputs:
0000000000000270 S __D3one3two5three__T14StructTemplateTS3oneQBjQBi1BZQBf6__initZ
U __D3one3two5three__T14StructTemplateTSQBjQBiQBh1BZQBe6__initZ
00000000000001b8 S __D60TypeInfo_S3one3two5three__T14StructTemplateTSQBjQBiQBh1BZQBe6__initZ
As you can see there is an undefined symbol: the one that we defined using pragma(mangle). The mangling is just slightly off to what the compiler defines. Note that the defined symbol `__D3one3two5three__T14StructTemplateTS3oneQBjQBi1BZQBf6__initZ` contains "one" twice. Somehow, the repeated identifier detection/encoding does not work. It _does_ work for the TypeInfo symbol (and all other symbols related to `StructTemplate!B`.
This inconsistency between `T.mangleof` and the init symbol mangling is a blocker for implementing a druntime hack to access the init symbol similar to this article: http://dpldocs.info/this-week-in-d/Blog.Posted_2020_07_27.html#zero-runtime-classes
Comment #1 by dlang-bot — 2020-08-29T15:05:32Z
@kinke created dlang/dmd pull request #11645 "Fix Issue 21120 - Inconsistent mangling of __init symbol" fixing this issue:
- Fix Issue 21120 - Inconsistent mangling of __init symbol
The mangling code previously assumed no identifier would ever start at
buffer offset 0. Get rid of that assumption by storing offset+1.
https://github.com/dlang/dmd/pull/11645
Comment #2 by dlang-bot — 2020-08-29T17:19:04Z
dlang/dmd pull request #11645 "Fix Issue 21120 - Inconsistent mangling of __init symbol" was merged into stable:
- 4e863aa3532480abe5832b2448904bdf8e441ddf by Martin Kinkelin:
Fix Issue 21120 - Inconsistent mangling of __init symbol
The mangling code previously assumed no identifier would ever start at
buffer offset 0. Get rid of that assumption by storing offset+1.
https://github.com/dlang/dmd/pull/11645
Comment #3 by dlang-bot — 2020-09-11T06:12:23Z
dlang/dmd pull request #11718 "merge stable" was merged into master:
- 28c0276975b9c395af0b7a8912a7c1d074166768 by Martin Kinkelin:
Fix Issue 21120 - Inconsistent mangling of __init symbol
The mangling code previously assumed no identifier would ever start at
buffer offset 0. Get rid of that assumption by storing offset+1.
https://github.com/dlang/dmd/pull/11718