0

Currently using Microsoft Visual Studio Community 2019 Version 16.4.4 and compiling a C project.

I want to be able to output assembly from VS, make modifications to the assembly, and then compile the modified assembly into an executable file. Below I talk about my attempts following instructions from a previous SO question, but if there is an alternative (even not using VS) I would appreciate those suggestions.

As a test, I have been using a simple "hello world" program:

#include <stdio.h>
int main(int argc, char* argv) {
    printf("Hello world\n");
    return 0;
}

I have tried compiling this program following the instructions given by this answer but I get the following errors:

LNK2005 ___local_stdio_printf_options already defined in helloworld.obj

LNK1169 one or more multiply defined symbols found

It looks like VS is actually compiling the assembly to object files, but the linker says that the "___local_stdio_printf_options" symbol is already defined at link-time. When I look back at the assembly code output from VS, this seems to be true:

___local_stdio_printf_options PROC          ; COMDAT
; File C:\Program Files (x86)\Windows Kits\10\Include\10.0.18362.0\ucrt\corecrt_stdio_config.h
; Line 86
    push    ebp
    mov ebp, esp
; Line 88
    mov eax, OFFSET ?_OptionsStorage@?1??__local_stdio_printf_options@@9@9 ; `__local_stdio_printf_options'::`2'::_OptionsStorage
; Line 89
    pop ebp
    ret 0
___local_stdio_printf_options ENDP

So it looks like the a solution for this may be to prevent the VS compiler from defining ___local_stdio_printf_options or change the linker setting to accept this style of coding somehow. I don't know why VS would produce code that defines external symbols like this. I would really appreciate any help.

Community
  • 1
  • 1
  • 1
    So you're trying to override the MS internal library function `___local_stdio_printf_options`? Or is that just left-over in MSVC's asm output? Unlike GCC/clang, I think I've read that MSVC's asm output isn't really intended to be assembled. In this case that would mean hand-editing to remove functions like this that the library already defines. – Peter Cordes Feb 13 '20 at 07:33
  • @PeterCordes Yeah, that definition is left over from MSVC's output. I guess if hand-editing is what I have to do I'll try it. I'm sort of surprised there isn't an easier way to do something like this... I would expect MSVC's compiler to produce assembly and compile that assembly during the normal compilation process? – kansas_bulldog382 Feb 13 '20 at 21:20
  • 1
    No, like most compilers it compiles straight to machine code. GCC is one of the only widely-used compilers that still always compiles to asm text and feeds that to a separate assembler as part of normal compilation. MSVC is weird in that it can't produce asm you can use, even if you want it. If you don't like that, use a better compiler. – Peter Cordes Feb 13 '20 at 22:49
  • Gotcha. Thanks for the insights! – kansas_bulldog382 Feb 14 '20 at 04:53

0 Answers0