www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 5337] New: DMD compiles incorrect code on windows

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5337

           Summary: DMD compiles incorrect code on windows
           Product: D
           Version: D2
          Platform: x86
        OS/Version: Windows
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: CrypticMetaphor88 gmail.com



Created an attachment (id=845)
Compiling and running in windows command prompt

Code that should not compile, imported from C, compiles on windows, but does
NOT compile on Linux.

Here the example:
// cfile.c file
extern int globalFromD;

void functionFromC(int a) {
   globalFromD = a;
}
// end cfile.c

// dfile.d
extern(C) { // this is needed to make it available from C
        int globalFromD;
}
extern(C) { // also needed when listing the prototypes for your C functions
        void functionFromC(int);
}

import std.stdio; // for writefln

int main() {
        globalFromD = 100;
        writefln("%d", globalFromD);

        functionFromC(500);
        writefln("%d", globalFromD);

        return 0;
}
// end dfile.d

I compile with:
dmc -c cfile.c
And I get  an cfile.obj, which is the object code (.o in gcc).
Then I compile the D code
dmd dfile.d cfile.obj
and I get no errors, so I run it, the result:
// start result
C:\DCode\libtest>dfile.exe
100
100

C:\DCode\libtest>
// end result 

I've posted this on the newsgroup( d.D.learn, Subject:"Calling C functions" )
and apparently it's some kind of bug.

More info:

dmc version: 8.42n
dmd version: 2.050

Windows version: Windows XP.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Dec 09 2010
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5337


Steven Schveighoffer <schveiguy yahoo.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |wrong-code
                 CC|                            |schveiguy yahoo.com



08:56:03 PST ---
I think this is a TLS issue.

The linux errors I got (repeated from NG post):

steves steve-laptop:~/testd$ gcc -c testc.c
steves steve-laptop:~/testd$ ~/dmd-2.050/linux/bin/dmd testcallc.d testc.o
/usr/bin/ld: globalFromD: TLS definition in testcallc.o section .tbss
mismatches non-TLS reference in testc.o
testc.o: could not read symbols: Bad value
collect2: ld returned 1 exit status
--- errorlevel 1

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Dec 09 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5337


Walter Bright <bugzilla digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |bugzilla digitalmars.com
         Resolution|                            |INVALID



10:16:40 PST ---
It is a TLS issue. Globals in D2 are allocated in thread local storage, globals
in C are not. To make it work, they have to be TLS in both languages, or
regular globals in both.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Dec 09 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5337




10:19:27 PST ---
So why can the linux compiler detect the issue and not the windows compiler?

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Dec 09 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5337


nfxjfg gmail.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |nfxjfg gmail.com



Maybe because OPTLINK is a dirty piece of shit and eats any crap without even
spitting out warnings?

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Dec 09 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5337




13:35:59 PST ---
The linux compiler is not detecting the error, the linker is.

The way TLS is implemented is radically different on Windows, Linux, and OSX.
Linux does it with special linker fixups, Windows does it with specially
generated code. The Windows linker cannot tell that it is supposed to be TLS
access.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Dec 09 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5337




 Walter: that's very unfortunate, this will cause lots of confusion among new
users. Maybe make extern(C) variables __gshared by default?

PS: optlink is a turd anyway. Also looking forward to optlink64.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Dec 09 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5337


Steven Schveighoffer <schveiguy yahoo.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|wrong-code                  |
             Status|RESOLVED                    |REOPENED
          Component|DMD                         |websites
         Resolution|INVALID                     |
         AssignedTo|nobody puremagic.com        |schveiguy yahoo.com



16:11:48 PST ---
thanks for the explanation.

Perhaps this page needs to note these problems? 
http://www.digitalmars.com/d/2.0/interfaceToC.html

I'll see if I can do that, I'm going to update this bug as a documentation
issue.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Dec 09 2010
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5337


Walter Bright <bugzilla digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|REOPENED                    |RESOLVED
         Resolution|                            |FIXED



13:31:32 PST ---
Fixed interfaceToC.html 2.058/1.073

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jan 20 2012