Skip to content

[LLD] [MinGW] Interpret an empty -entry option as no entry point #96055

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 20, 2024

Conversation

mstorsjo
Copy link
Member

@mstorsjo mstorsjo commented Jun 19, 2024

This fixes #93309, and seems to match how GNU ld handles this case.

@mstorsjo mstorsjo requested a review from alvinhochun June 19, 2024 10:59
@llvmbot llvmbot added the lld label Jun 19, 2024
@llvmbot
Copy link
Member

llvmbot commented Jun 19, 2024

@llvm/pr-subscribers-lld

@llvm/pr-subscribers-lld-coff

Author: Martin Storsjö (mstorsjo)

Changes

This fixes #93309, and seems to match how GNU ld handles this case.

Also treat a missing -entry argument as no entry point; this also is what GNU ld does in this case.


Full diff: https://github.com/llvm/llvm-project/pull/96055.diff

2 Files Affected:

  • (modified) lld/MinGW/Driver.cpp (+5-1)
  • (modified) lld/test/MinGW/driver.test (+3)
diff --git a/lld/MinGW/Driver.cpp b/lld/MinGW/Driver.cpp
index 0d55d5b3672a4..0527839ba6f02 100644
--- a/lld/MinGW/Driver.cpp
+++ b/lld/MinGW/Driver.cpp
@@ -223,8 +223,12 @@ bool link(ArrayRef<const char *> argsArr, llvm::raw_ostream &stdoutOS,
     StringRef s = a->getValue();
     if (args.getLastArgValue(OPT_m) == "i386pe" && s.starts_with("_"))
       add("-entry:" + s.substr(1));
-    else
+    else if (!s.empty())
       add("-entry:" + s);
+    else
+      add("-noentry");
+  } else {
+    add("-noentry");
   }
 
   if (args.hasArg(OPT_major_os_version, OPT_minor_os_version,
diff --git a/lld/test/MinGW/driver.test b/lld/test/MinGW/driver.test
index 619fee8dee7c1..ef5fc8b4aa905 100644
--- a/lld/test/MinGW/driver.test
+++ b/lld/test/MinGW/driver.test
@@ -60,6 +60,9 @@ RUN: ld.lld -### foo.o -m i386pep --entry bar 2>&1 | FileCheck -check-prefix=ENT
 RUN: ld.lld -### foo.o -m i386pep -entry=bar 2>&1 | FileCheck -check-prefix=ENTRY %s
 RUN: ld.lld -### foo.o -m i386pep --entry=bar 2>&1 | FileCheck -check-prefix=ENTRY %s
 ENTRY: -entry:bar
+RUN: ld.lld -### foo.o -m i386pep -e bar -entry= 2>&1 | FileCheck -check-prefix=NOENTRY --implicit-check-not=-entry %s
+RUN: ld.lld -### foo.o -m i386pep 2>&1 | FileCheck -check-prefix=NOENTRY %s
+NOENTRY: -noentry
 
 RUN: ld.lld -### foo.o -m i386pep -mllvm bar -mllvm baz 2>&1 | FileCheck -check-prefix=MLLVM %s
 MLLVM: -mllvm:bar -mllvm:baz

mstorsjo added a commit to mstorsjo/llvm-project that referenced this pull request Jun 19, 2024
We can't pass an empty string to addUndefined().

This fixes the crash that was encountered in
llvm#93309 (turning the
crash into a properly handled error; making it do the right
thing is handled in llvm#96055).
@alvinhochun
Copy link
Contributor

Also treat a missing -entry argument as no entry point; this also is what GNU ld does in this case.

Not sure if this is necessary or correct. Wouldn't this break the default entry points (e.g. WinMainCRTStartup/mainCRTStartup)?

@mstorsjo
Copy link
Member Author

Also treat a missing -entry argument as no entry point; this also is what GNU ld does in this case.

Not sure if this is necessary or correct. Wouldn't this break the default entry points (e.g. WinMainCRTStartup/mainCRTStartup)?

There's indeed no strict need for this part of the change, I just chose to include it for completeness (as I remember noticing this behaviour in GNU ld before). And yes, you're right that it would change the behaviour of default entry points.

However the default entry point behaviour never really comes into play in mingw configurations; as long as you drive the linking via GCC/Clang, it always passes one -e option or another, so it practically never will hit that case anyway. (And that choice depends on whether you're passing -municode when linking.) So practically, the case only matters if you're invoking ld.lld directly.

If you feel it's nice to keep the lld style autodetection here, we could of course do that, but practically it makes not much difference either way.

@alvinhochun
Copy link
Contributor

as long as you drive the linking via GCC/Clang, it always passes one -e option or another, so it practically never will hit that case anyway

Does it...? When I run clang++.exe -### -x c++ - I don't see any entry point flags in the ld.lld invocation.

@mstorsjo
Copy link
Member Author

as long as you drive the linking via GCC/Clang, it always passes one -e option or another, so it practically never will hit that case anyway

Does it...? When I run clang++.exe -### -x c++ - I don't see any entry point flags in the ld.lld invocation.

Oh, you're absolutely right, it isn't - I clearly misremembered, and didn't check correctly.

Indeed, we shouldn't be touching this aspect of it - the default behaviour in GNU ld is also more nuanced than this.

@mstorsjo mstorsjo force-pushed the lld-mingw-noentry branch from 71693ac to 95b091f Compare June 19, 2024 12:59
This fixes llvm#93309,
and seems to match how GNU ld handles this case.
@mstorsjo mstorsjo force-pushed the lld-mingw-noentry branch from 95b091f to 373469f Compare June 19, 2024 12:59
@mstorsjo
Copy link
Member Author

as long as you drive the linking via GCC/Clang, it always passes one -e option or another, so it practically never will hit that case anyway

Does it...? When I run clang++.exe -### -x c++ - I don't see any entry point flags in the ld.lld invocation.

Oh, you're absolutely right, it isn't - I clearly misremembered, and didn't check correctly.

Indeed, we shouldn't be touching this aspect of it - the default behaviour in GNU ld is also more nuanced than this.

Pushed an update to remove this part of the change now.

Copy link
Contributor

@alvinhochun alvinhochun left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, LGTM.

@mstorsjo mstorsjo merged commit acf675b into llvm:main Jun 20, 2024
7 checks passed
@mstorsjo mstorsjo deleted the lld-mingw-noentry branch June 20, 2024 08:20
mstorsjo added a commit that referenced this pull request Jun 24, 2024
We can't pass an empty string to addUndefined().

This fixes the crash that was encountered in
#93309 (turning the crash
into a properly handled error; making it do the right thing is handled
in #96055).
AlexisPerry pushed a commit to llvm-project-tlp/llvm-project that referenced this pull request Jul 9, 2024
…m#96055)

This fixes llvm#93309, and seems
to match how GNU ld handles this case.
AlexisPerry pushed a commit to llvm-project-tlp/llvm-project that referenced this pull request Jul 9, 2024
We can't pass an empty string to addUndefined().

This fixes the crash that was encountered in
llvm#93309 (turning the crash
into a properly handled error; making it do the right thing is handled
in llvm#96055).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Compiling a shared library with -Wl,-entry= causes a crash
3 participants