Thread overview
Why export external(C) generate mangled names?
Oct 25, 2019
Suliman
Oct 25, 2019
Daniel Kozak
Oct 25, 2019
Daniel Kozak
Oct 25, 2019
Simen Kjærås
Oct 25, 2019
mipri
October 25, 2019
I thought that export extern(C) is needed for C compatibility. But When I am compiling follow code

export extern(C)
{
    void foo()
    {
    }
}

I am getting section that have mangled names:

� foo.d �  �7nO �  �CV �,  FLAT_TEXTCODE_DATADATACONST_BSSBSS �	 �     �	 i     �	 i    �	 i    	 �  � _foo �
  �  � _foo  � FMBFMFME �	 �     �	 �    �	 �
 � __D3foo12__ModuleInfoZ      �
          
� �           foo  �
          � �  �

Why is __D3foo12__ModuleInfoZ added to obj file?
October 25, 2019
On Fri, Oct 25, 2019 at 12:35 PM Suliman via Digitalmars-d <digitalmars-d@puremagic.com> wrote:
>
> I thought that export extern(C) is needed for C compatibility.
> But When I am compiling follow code
>
> export extern(C)
> {
>      void foo()
>      {
>      }
> }
>
> I am getting section that have mangled names:
>
> �   foo.d �   �7nO �   � CV �,
>  FLAT _TEXT CODE _DATA DATA CONST _BSS BSS �     �        �      i
>     �    i        �      i        �    �   _foo �
>    �   �   _foo  �   FMB FM FME �        �        �      �       �       �
>    �     __D3foo12__ModuleInfoZ      �
>
> � �               foo  �
>             �  �     �
>
> Why is __D3foo12__ModuleInfoZ added to obj file?

Try to compile with -betterC

October 25, 2019
On Fri, Oct 25, 2019 at 12:56 PM Daniel Kozak <kozzi11@gmail.com> wrote:
>
> On Fri, Oct 25, 2019 at 12:35 PM Suliman via Digitalmars-d <digitalmars-d@puremagic.com> wrote:
> >
> > I thought that export extern(C) is needed for C compatibility.
> > But When I am compiling follow code
> >
> > export extern(C)
> > {
> >      void foo()
> >      {
> >      }
> > }
> >
> > I am getting section that have mangled names:
> >
> > �   foo.d �   �7nO �   � CV �,
> >  FLAT _TEXT CODE _DATA DATA CONST _BSS BSS �     �        �      i
> >     �    i        �      i        �    �   _foo �
> >    �   �   _foo  �   FMB FM FME �        �        �      �       �       �
> >    �     __D3foo12__ModuleInfoZ      �
> >
> > � �               foo  �
> >             �  �     �
> >
> > Why is __D3foo12__ModuleInfoZ added to obj file?
>
> Try to compile with -betterC

You should get:

[kozak@missandei ~]$ dmd -c -ofobj.o obj.d
[kozak@missandei ~]$ nm obj.o
0000000000000000 t
                 U _d_dso_registry
0000000000000000 R _D3obj12__ModuleInfoZ
0000000000000000 W foo
                 U __start_minfo
                 U __stop_minfo

[kozak@missandei ~]$ dmd -betterC -c -ofobj.o obj.d
[kozak@missandei ~]$ nm obj.o
0000000000000000 t
0000000000000000 W foo

October 25, 2019
On Friday, 25 October 2019 at 10:31:15 UTC, Suliman wrote:
> I thought that export extern(C) is needed for C compatibility. But When I am compiling follow code
>
> export extern(C)
> {
>     void foo()
>     {
>     }
> }
>
> I am getting section that have mangled names:
>
> � foo.d �  �7nO �  �CV �,  FLAT_TEXTCODE_DATADATACONST_BSSBSS �	 �     �	 i     �	 i    �	 i    	 �  � _foo �
>   �  � _foo  � FMBFMFME �	 �     �	 �    �	 �
>  � __D3foo12__ModuleInfoZ      �
>           
> � �           foo  �
>           � �  �
>
> Why is __D3foo12__ModuleInfoZ added to obj file?


Is your module (source file) called foo? Because that name is for the ModuleInfo of a module by that name, and has nothing to do with the function called the same.

Like Daniel said, -betterc inhibits the creation of moduleinfo, so if that's what you're after, you should use it.

--
  Simen
October 25, 2019
On Friday, 25 October 2019 at 10:31:15 UTC, Suliman wrote:
> I thought that export extern(C) is needed for C compatibility.

This is only the case for toplevel functions. If there's any nesting,
you still get D mangling but you get C calling conventions.

> But When I am compiling follow code
>
> export extern(C)
> {
>     void foo()
>     {
>     }
> }

There's no mangling with this code for me. I have to add a struct {}
around it to see mangling.

>
> I am getting section that have mangled names:
>

Look with: objdump -dwr --no-show-raw-insn <filename>

You'll get output like

  000000000042d5f8 <foo>:
    42d5f8:	push   %rbp
    42d5f9:	mov    %rsp,%rbp
    42d5fc:	pop    %rbp
    42d5fd:	retq
  	...

  000000000042d600 <_D4x1244test3barMUZv>:
    42d600:	push   %rbp
    42d601:	mov    %rsp,%rbp
    42d604:	pop    %rbp
    42d605:	retq

for

  extern(C) void foo() {}

  struct test {
      export extern(C) {
          void bar() {}
      }
  }

A note about this was added to the documentation with

https://github.com/dlang/dlang.org/pull/1911/files

but it might be easy to miss since it's not in InterfaceToC