Skip to contentSkip to conversation

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

PATCH In review Peter Korsgaard 1 participant 2 messages Jan 6, 2025 – Feb 3, 2025

lore
Share & tools
lore
#0 lore raw

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

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.

---
 toolchain/toolchain-wrapper.c | 21 +++++++++++----------
 1 file changed, 11 insertions(+), 10 deletions(-)
toolchain/toolchain-wrapper.c +11 -10
index db11cdb6ab..8b1fb79dde 100644
@@ -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";
Signature · 6 lines
2.39.5

_______________________________________________
buildroot mailing list
[email protected]
https://lists.buildroot.org/mailman/listinfo/buildroot
#1 lore raw
Quoted 25 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

"Peter" == Peter Korsgaard <[email protected]> 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.

Quoted 5 lines

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.

Committed, thanks.

Signature · 5 lines
Bye, Peter Korsgaard
_______________________________________________
buildroot mailing list
[email protected]
https://lists.buildroot.org/mailman/listinfo/buildroot