Commit 86e5bbffd7

LemonBoy <thatlemon@gmail.com>
2019-10-09 22:41:38
Patch lld to have a more sensible kill-at implementation
Lift some code from llvm-dlltool, the lld code is meant to follow what gnu ld does but that's not much useful for our purposes. Also use the `--kill-at` option when generating the .lib files out of mingw's .def files: this way our building process closely matches the one use by the upstream and now finally generates files that allow both C code and Zig code to link.
1 parent c9a3c94
Changed files (2)
deps
src
deps/lld/COFF/DriverUtils.cpp
@@ -638,10 +638,18 @@ void fixupExports() {
 
   if (config->killAt && config->machine == I386) {
     for (Export &e : config->exports) {
-      e.name = killAt(e.name, true);
-      e.exportName = killAt(e.exportName, false);
-      e.extName = killAt(e.extName, true);
-      e.symbolName = killAt(e.symbolName, true);
+      if (!e.name.empty() && e.name[0] == '?')
+        continue;
+      e.symbolName = e.name;
+      // Trim off the trailing decoration. Symbols will always have a
+      // starting prefix here (either _ for cdecl/stdcall, @ for fastcall
+      // or ? for C++ functions). Vectorcall functions won't have any
+      // fixed prefix, but the function base name will still be at least
+      // one char.
+      e.name = e.name.substr(0, e.name.find('@', 1));
+      // By making sure E.SymbolName != E.Name for decorated symbols,
+      // writeImportLibrary writes these symbols with the type
+      // IMPORT_NAME_UNDECORATE.
     }
   }
 
src/link.cpp
@@ -2057,10 +2057,19 @@ static const char *get_def_lib(CodeGen *parent, const char *name, Buf *def_in_fi
         args.resize(0);
         args.append("link");
         coff_append_machine_arg(parent, &args);
+        args.append("-lldmingw");
+        args.append("-kill-at");
 
         args.append(buf_ptr(buf_sprintf("-DEF:%s", buf_ptr(def_final_path))));
         args.append(buf_ptr(buf_sprintf("-OUT:%s", buf_ptr(lib_final_path))));
 
+        if (parent->verbose_link) {
+            for (size_t i = 0; i < args.length; i += 1) {
+                fprintf(stderr, "%s ", args.at(i));
+            }
+            fprintf(stderr, "\n");
+        }
+
         Buf diag = BUF_INIT;
         ZigLLVM_ObjectFormatType target_ofmt = target_object_format(parent->zig_target);
         if (!zig_lld_link(target_ofmt, args.items, args.length, &diag)) {