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

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
9954315fc5 ("toolchain/toolchain-wrapper: make gcc --help -v work
correctly"), so drop that.

Signed-off-by: Peter Korsgaard <peter@korsgaard.com>
This commit is contained in:
Peter Korsgaard
2025-01-06 12:01:29 +01:00
parent 85376a7181
commit ab0aac8179

View File

@@ -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";