D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 8207 - OS X: Should extern(D) symbols include another underscore?
Summary: OS X: Should extern(D) symbols include another underscore?
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All Mac OS X
: P2 normal
Assignee: No Owner
URL:
Keywords:
: 15748 (view as issue list)
Depends on:
Blocks: 8172
  Show dependency treegraph
 
Reported: 2012-06-07 15:30 UTC by David Nadlinger
Modified: 2018-02-10 23:26 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description David Nadlinger 2012-06-07 15:30:14 UTC
On OS X, all symbol names by default have a leading underscore. For example, the C `printf` function is represented as `_printf`. This mangling scheme is expected at least by the system GDB and several other tools, which strip the leading underscore when rendering symbol names.

Currently (DMD 2.060 Git), however, extern(D) symbols only have a single leading underscore as well. For example, `void foo()` in a module called test becomes `_D4test3fooFZv`, which in turn would be displayed by GDB as `D4test3fooFZv`

This seems wrong, as the D ABI requires a leading underscore as part of the symbol name itself. If another underscore was added, it would e.g. the above function to be correctly displayed in GDB as `_D4test3fooFZv`.

This is not merely a cosmetic issue, as other compilers have to follow the same ABI.

My proposal would be to modify the Mach obj backend of DMD to also emit the additional underscore, instead of modifying LDC (and possibly GDC as well), because:
 1) It would cause the names to be displayed as described in the spec in the tools.
 2) The LLVM toolchain adds the underscore automatically, which seems to be a strong indication of the common practice, and 
 3) indeed, GCC also emits C++ functions with two underscores, e.g. __ZN4llvm10error_code7successEv (and the ABI describes the mangling with a single leading underscore as well).

I already have a set of changes for this ready to be submitted as a pull request, in case this route is agreed on.
Comment 1 David Nadlinger 2012-06-07 16:37:16 UTC
The D demangling code in newer GDB versions also seems to expect two underscores, see issue 8172. Will try to test my changes more thoroughly and put together a pull request asap.
Comment 2 Dan Olson 2015-04-21 07:02:20 UTC
I would think yes, D symbols emitted to the object file on OS X should have extra underscore.  And .mangleof should produce the symbol without the extra underscore as it does today. 

extern(C++) has opposite problem.  The extra underscore is present in object file (correct) but is also present in .mangleof symbol.  It should be stripped from the .mangleof symbol.  Note: ldc merge-2.067 does this now which caused test compilable/test7030.d to fail, which is how I got here.  I think LDC is correct.

-- demo --
void dfun(int);
extern(C++) void cxxfun(int);
extern (C) void cfun(int);

pragma(msg, dfun.mangleof);
pragma(msg, cxxfun.mangleof);
pragma(msg, cfun.mangleof);

dmd 2.067 produces:
_D10showmangle4dfunFiZv
__Z6cxxfuni
cfun

where obj file has these symbols:
                 U _D10showmangle4dfunFiZv
                 U __Z6cxxfuni
                 U _cfun
Comment 3 David Nadlinger 2017-09-28 18:31:58 UTC
*** Issue 15748 has been marked as a duplicate of this issue. ***
Comment 4 github-bugzilla 2018-02-10 23:26:42 UTC
Commits pushed to master at https://github.com/dlang/dmd

https://github.com/dlang/dmd/commit/db2976599db44f2163986ba9cb541afd7cfa8471
fix Issue 8207 - extern(D) symbols should include another underscore

    1. Removed ABI name fiddling from the front-end.
    2. Removed `Target.prefixName` and C++ tests for matching `__Z`.
    3. Prepend prefixes for C++ and D symbols in the backend only
       for the targets that require it (Win32, OSX).
    4. Removed `RTLSYM__DINVARIANT` runtime library symbol, name is now
       the same on all targets.
    5. Correctly set C++ mangling on `LINK.cpp` symbols, before they
       were all being set as D mangled symbols.
    6. Use System mangling as synonym for do not modify symbol.
    7. Added workaround for `extern(D) ___tls_get_addr`.

https://github.com/dlang/dmd/commit/9691eba9441f7f165359716f80f46486ea09fb46
Merge pull request #7620 from ibuclaw/ingcc2

fix Issue 8207 - extern(D) symbols should include another underscore
merged-on-behalf-of: unknown