diff mbox series

toolchain/toolchain-wrapper.c: do not pass linker flags if not linking

Message ID 20250106110130.2837398-1-peter@korsgaard.com
State Accepted
Headers show
Series toolchain/toolchain-wrapper.c: do not pass linker flags if not linking | expand

Commit Message

Peter Korsgaard Jan. 6, 2025, 11:01 a.m. UTC
Fixes: https://gitlab.com/buildroot.org/buildroot/-/issues/55

When gcc sees a linker option (-Wl,..) it executes the linker, leading to
confusing error messages if no source files are provided, E.G.:

% gcc
gcc: fatal error: no input files
compilation terminated.

% gcc -Wl,-z,now
/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/12/../../../x86_64-linux-gnu/Scrt1.o: in function `_start':
(.text+0x17): undefined reference to `main'
collect2: error: ld returned 1 exit status

It is not really trivial to detect in the wrapper if linking will be done,
but we can at least check if any non-option arguments (E.G.  source/object
files) are passed and skip the -Wl,.. options if not.

% ./host/bin/aarch64-linux-gcc
aarch64-linux-gcc.br_real: fatal error: no input files
compilation terminated.

With this fixed we no longer need the special case for --help -v from commit
9954315fc5b2 ("toolchain/toolchain-wrapper: make gcc --help -v work
correctly"), so drop that.

Signed-off-by: Peter Korsgaard <peter@korsgaard.com>
---
 toolchain/toolchain-wrapper.c | 21 +++++++++++----------
 1 file changed, 11 insertions(+), 10 deletions(-)

Comments

Peter Korsgaard Feb. 3, 2025, 11:42 a.m. UTC | #1
>>>>> "Peter" == Peter Korsgaard <peter@korsgaard.com> writes:

 > Fixes: https://gitlab.com/buildroot.org/buildroot/-/issues/55
 > When gcc sees a linker option (-Wl,..) it executes the linker, leading to
 > confusing error messages if no source files are provided, E.G.:

 > % gcc
 > gcc: fatal error: no input files
 > compilation terminated.

 > % gcc -Wl,-z,now
 > /usr/bin/ld:
 > /usr/lib/gcc/x86_64-linux-gnu/12/../../../x86_64-linux-gnu/Scrt1.o: in
 > function `_start':
 > (.text+0x17): undefined reference to `main'
 > collect2: error: ld returned 1 exit status

 > It is not really trivial to detect in the wrapper if linking will be done,
 > but we can at least check if any non-option arguments (E.G.  source/object
 > files) are passed and skip the -Wl,.. options if not.

 > % ./host/bin/aarch64-linux-gcc
 > aarch64-linux-gcc.br_real: fatal error: no input files
 > compilation terminated.

 > With this fixed we no longer need the special case for --help -v from commit
 > 9954315fc5b2 ("toolchain/toolchain-wrapper: make gcc --help -v work
 > correctly"), so drop that.

 > Signed-off-by: Peter Korsgaard <peter@korsgaard.com>

Committed, thanks.
diff mbox series

Patch

diff --git a/toolchain/toolchain-wrapper.c b/toolchain/toolchain-wrapper.c
index db11cdb6ab..8b1fb79dde 100644
--- a/toolchain/toolchain-wrapper.c
+++ b/toolchain/toolchain-wrapper.c
@@ -245,7 +245,7 @@  int main(int argc, char **argv)
 	char *progpath = argv[0];
 	char *basename;
 	char *env_debug;
-	int ret, i, count = 0, debug = 0, found_shared = 0;
+	int ret, i, count = 0, debug = 0, found_shared = 0, found_nonoption = 0;
 
 	/* Debug the wrapper to see arguments it was called with.
 	 * If environment variable BR2_DEBUG_WRAPPER is:
@@ -311,13 +311,11 @@  int main(int argc, char **argv)
 		return 3;
 	}
 
-	/* skip all processing --help is specified */
+	/* any non-option (E.G. source / object files) arguments passed? */
 	for (i = 1; i < argc; i++) {
-		if (!strcmp(argv[i], "--help")) {
-			argv[0] = path;
-			if (execv(path, argv))
-				perror(path);
-			return 1;
+		if (argv[i][0] != '-') {
+			found_nonoption = 1;
+			break;
 		}
 	}
 
@@ -342,8 +340,11 @@  int main(int argc, char **argv)
 	}
 
 	/* start with predefined args */
-	memcpy(cur, predef_args, sizeof(predef_args));
-	cur += sizeof(predef_args) / sizeof(predef_args[0]);
+	for (i = 0; i < sizeof(predef_args) / sizeof(predef_args[0]); i++) {
+		/* skip linker flags when we know we are not linking */
+		if (found_nonoption || strncmp(predef_args[i], "-Wl,", strlen("-Wl,")))
+			*cur++ = predef_args[i];
+	}
 
 #ifdef BR_FLOAT_ABI
 	/* add float abi if not overridden in args */
@@ -463,7 +464,7 @@  int main(int argc, char **argv)
 		    !strcmp(argv[i], "-D__UBOOT__"))
 			break;
 	}
-	if (i == argc) {
+	if (i == argc && found_nonoption) {
 		/* https://wiki.gentoo.org/wiki/Hardened/Toolchain#Mark_Read-Only_Appropriate_Sections */
 #ifdef BR2_RELRO_PARTIAL
 		*cur++ = "-Wl,-z,relro";