www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 23343] New: ImportC: functions declared with asm label to set

https://issues.dlang.org/show_bug.cgi?id=23343

          Issue ID: 23343
           Summary: ImportC: functions declared with asm label to set
                    symbol name gets extra underscore prepended
           Product: D
           Version: D2
          Hardware: All
                OS: Mac OS X
            Status: NEW
          Keywords: ImportC, link-failure
          Severity: normal
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: dave287091 gmail.com

Lots of libc functions on macOS are declared like:

  int open(const char *, int, ...) asm("_" "open" );

with the asm label to (pointlessly) set the symbol name.

Apparently an extra underscore gets prepended at some point as the following C
program will fail to link:

---

// o.c
int open(const char*, int, ...) asm("_" "open");

int main(){
    int fd = open("/dev/null", 0);
    return fd >= 0 ? 0 : 1;
}

---

$ dmd o.c

Undefined symbols for architecture x86_64:
  "__open", referenced from:
      _main in o.o
ld: symbol(s) not found for architecture x86_64

Strangely though, if you write a D file to print the mangle of `open`:

---

// op.d
import o;
pragma(msg, o.open.mangleof); // _open

---

The extra underscore is not present.

Output of `nm` also shows the extra underscore.

$ nm -g o.o
                 U __open
0000000000000000 T _main
0000000000000058 S _o.main.eh

I don’t know if this issue affects other platforms.

--
Sep 17 2022