Search code examples
visual-c++assemblymasm

Compile asm file with cl.exe to dll


Given: prog.c with an entry point prog

I normally do

cl.exe /MD /LD /Fe"prog.dll" /Fo"prog" "prog.c" /link ext.lib

or

cl.exe /MD /Fo"prog.obj"
cl.exe /MD /LD /Fe"prog.dll" "prog.obj" /link ext.lib

in both cases the resulting prog.dll works fine.

Now I did the following to get an asm file instead of the obj file:

cl.exe /c /MD /Fa"prog"

This "works" so far, too. But I cannot figure out how to make a dll of this file.

Tried:

ml.exe /c /Cx /coff prog.asm
cl.exe /MD /LD /Fe"prog.dll" "prog.obj" /link ext.lib

Result: prog.dll without entry point prog

Tried again:

ml.exe /c /Cx /coff prog.asm
cl.exe /MD /LD /Fe"prog.dll" "prog.obj" /link /entry:prog ext.lib

Result: compiler warning about wrong entry point _prog not being stdcall with 12 byte arguments and a compiler error about unresolved symbol _memcpy.

Question: Is there any way to compile the asm file which cl.exe generates by /Fa to a dll (preferably via cl.exe, if not possible with ml.exe)?


Solution

  • Is there any way to compile the asm file which cl.exe generates by /Fa to a dll (preferably via cl.exe, if not possible with ml.exe)?

    No:

    1. The C/C++ compiler (cl.exe) cannot assemble assembly-code input. It takes only C or C++ source code as input. The assembler is MASM (ml.exe).
    2. The assembly-code output of cl.exe cannot, in general, be fed directly into MASM. In some cases, it is not even valid assembly code. In other cases, there are directives, keywords, and other things emitted in the code that MASM doesn't directly support. Things get especially hairy if the C/C++ source uses exceptions. The listing file is for informational purposes only.

    It is very unclear to me why you are even wanting to do this in the first place. If your source code is either C or C++, and can be compiled and linked by MSVC, then what is the point of introducing the additional intermediate step of converting it to and from assembly language? Just use cl.exe directly to make a DLL.

    If you absolutely must do this, you will have to take the ASM listing file generated by MSVC and manually clean it up before running it through MASM. You can make this clean-up task easier by turning off whole program optimization, turning off exception handling, turning off security checks/cookies, and indicating to the linker that the image does not contain safe SEH handlers. Note that some of these may break or change the behavior of your code! You'll also need to add EXTERN definitions for functions that you call from runtime libraries.