[PATCH] D37710: [LLD] [MinGW] Pass the undecorated entry point name to the COFF linker

Martin Storsjö via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 12 00:57:03 PDT 2017


mstorsjo added a comment.

In https://reviews.llvm.org/D37710#867043, @ruiu wrote:

> lld COFF linker also does a trick for the leading underscore, so you may not need to handle that in the shim driver. Can't you link your program without line 131-133?


There's lots of tricks back and forth for the leading underscore - the COFF linker unconditionally adds it here:

  // Symbol names are mangled by appending "_" prefix on x86.
  StringRef LinkerDriver::mangle(StringRef Sym) {
    assert(Config->Machine != IMAGE_FILE_MACHINE_UNKNOWN);
    if (Config->Machine == I386)
      return Saver.save("_" + Sym);
    return Sym;
  }
  
    // Handle /entry and /dll
    if (auto *Arg = Args.getLastArg(OPT_entry)) {
      Config->Entry = addUndefined(mangle(Arg->getValue()));

We can't make this to work with heuristics because then you'll pick the wrong one when there's ambiguity.

I tested the behaviour in GNU ld and link.exe like this:

  $ cat entry.c
  int puts(const char* str);
  int entry(void) {
    puts("entry no underscore");
    return 0;
  }
  int _entry(void) {
    puts("entry single underscore");
    return 0;
  }
  int __entry(void) {
    puts("entry double underscore");
    return 0;
  }
  
  $ cl entry.c -c -Foentry-msvc.o
  Microsoft (R) C/C++ Optimizing Compiler Version 19.10.25017 for x86
  Copyright (C) Microsoft Corporation.  All rights reserved.
  
  entry.c
  $ llvm-nm -g entry-msvc.o 
  00000040 T ___entry
  00000020 T __entry
  00000000 T _entry
           U _puts
  $ link entry-msvc.o -entry:entry -out:entry-link.exe -subsystem:console ucrt.lib
  $ wine entry-link.exe
  entry no underscore
  $ link entry-msvc.o -entry:_entry -out:entry-link.exe -subsystem:console ucrt.lib
  $ wine entry-link.exe
  entry single underscore
  
  $ i686-w64-mingw32-gcc -c entry.c -o entry-gcc.o
  $ i686-w64-mingw32-ld --entry entry entry-gcc.o -o entry-ld.exe -lmsvcrt
  i686-w64-mingw32-ld: warning: cannot find entry symbol entry; defaulting to 0000000000401000
  $ i686-w64-mingw32-ld --entry _entry entry-gcc.o -o entry-ld.exe -lmsvcrt
  $ wine entry-ld.exe
  entry no underscore
  $ i686-w64-mingw32-ld --entry __entry entry-gcc.o -o entry-ld.exe -lmsvcrt
  $ wine entry-ld.exe
  entry single underscore


https://reviews.llvm.org/D37710





More information about the llvm-commits mailing list