[LLVMdev] RFC: auto-linking IR proposal

Yunzhong Gao Yunzhong.Gao at am.sony.com
Wed Jan 16 13:14:22 PST 2013


Hi Daniel,

Nice to meet you.

My understanding of the Microsoft #pragma comment(lib, ...) semantics is that
each specified library will be converted into a directive that starts with
"/DEFAULTLIB" in the COFF .drectve section. To demonstrate, the following patch
produces directives that work with Visual Studio 2010 using the now-deprecated
dependent library feature (commits r168779 and r168694).

/* beginning of patch */

Index: /home/ygao/LLVM/llvm/lib/Target/X86/X86AsmPrinter.cpp
===================================================================
--- /home/ygao/LLVM/llvm/lib/Target/X86/X86AsmPrinter.cpp       (revision 165914)
+++ /home/ygao/LLVM/llvm/lib/Target/X86/X86AsmPrinter.cpp       (working copy)
@@ -641,6 +641,19 @@
     const TargetLoweringObjectFileCOFF &TLOFCOFF =
       static_cast<const TargetLoweringObjectFileCOFF&>(getObjFileLowering());
 
+    // Output linker support code for #pragma comment(lib, ...) on Windows
+    if (M.lib_size() > 0 && Subtarget->isTargetWindows()) {
+      OutStreamer.SwitchSection(TLOFCOFF.getDrectveSection());
+      SmallString<128> name;
+      for (Module::lib_iterator I = M.lib_begin(), E = M.lib_end();
+           I != E; ++I) {
+        name = " /DEFAULTLIB:\"";
+        name += *I;
+        name += "\"";
+        OutStreamer.EmitBytes(name, 0);
+      }
+    }
+
     for (Module::const_iterator I = M.begin(), E = M.end(); I != E; ++I)
       if (I->hasDLLExportLinkage())
         DLLExportedFns.push_back(Mang->getSymbol(I));

/* end of patch */

I am currently looking for advice on what is the correct approach to
re-implement #pragma comment(lib, ...) with Clang/LLVM.

A mingw linker (at least the one that I played with) just ignores this directive
with no warnings or errors.

One can either add another module flag for "Named Libraries" in addition to
"Linker Options", which might be easier to implement. Or one can provide a
target-specific hook to the frontend, which converts #pragma comment(lib,
"blah.lib") to something like this:

!0 = medata !{ i32 6, "Linker Options",
  metadata !{
    !metadata { metadata !"/DEFAULTLIB:blah.lib" }
  }
}
!llvm.module.flags = !{ !0 }

Your example is maybe more complicated where you need to convert libz to "-lz".

Regarding the ordering information of the libraries, I think for MSVC the
libraries are supposed to follow lexical order in the same source file. I am not
sure if there is any restriction across different source files. Here is a link
to the MSDN doc:
http://msdn.microsoft.com/en-us/library/7f0aews7%28v=vs.80%29.aspx

It is probably not too hard to add a directive section to ELF similar to COFF.

At the 2012 LLVM Dev Meeting, there was a presentation on adding Modules to C,
which presented a way to use ObjC-style .map files to specify libraries, which
may be relevant to this auto-linking feature.

I look forward to more discussions,

- Gao.





More information about the llvm-dev mailing list