-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Make: -nostartfiles -nodefaultlibs are not flags of LD but flags of GCC #3836
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't we also add these flags to CXXFLAGS
?
I believe adding those flags to CFLAGS is not the right option, since CFLAGS (and CXXFLAGS) relate to Compile flags. I believe that we should keep them as LDFLAGS and change the Linker command to call the GCC frontend ( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree with @gustavonihei, it's better to change LD to ${CROSSDEV}/gcc
As @gustavonihei suggest that the right fix is change LD to xxx-gcc. |
It is not so simple.
|
Yeah, these flags and some others are specific to LD, so when provided to the GCC frontend they must be provided with |
Other changes that I have locally here for a previous attempt to enable the LTO build: -LDSTARTGROUP ?= --start-group
-LDENDGROUP ?= --end-group
+LDSTARTGROUP ?= -Wl,--start-group
+LDENDGROUP ?= -Wl,--end-group
LDFLAGS += $(ARCHSCRIPT)
BOARDMAKE = $(if $(wildcard board$(DELIM)Makefile),y,)
@@ -103,7 +103,7 @@ board/libboard$(LIBEXT):
nuttx$(EXEEXT): $(STARTUP_OBJS) board/libboard$(LIBEXT)
@echo "LD: nuttx"
- $(Q) $(LD) --entry=__start $(LDFLAGS) $(LIBPATHS) $(EXTRA_LIBPATHS) \
+ $(Q) $(LD) -Wl,--entry=__start $(LDFLAGS) $(LIBPATHS) $(EXTRA_LIBPATHS) \
-o $(NUTTX) $(STARTUP_OBJS) $(EXTRA_OBJS) \
$(LDSTARTGROUP) $(LDLIBS) $(EXTRA_LIBS) $(LDENDGROUP)
ifneq ($(CONFIG_WINDOWS_NATIVE),y) ifeq ($(CONFIG_CYGWIN_WINTOOL),y)
- LDFLAGS += -Map="${shell cygpath -w $(TOPDIR)/nuttx.map}" --cref
+ LDFLAGS += -Wl,-Map="${shell cygpath -w $(TOPDIR)/nuttx.map}" -Wl,--cref
else
- LDFLAGS += -Map=$(TOPDIR)/nuttx.map --cref
+ LDFLAGS += -Wl,-Map=$(TOPDIR)/nuttx.map -Wl,--cref
endif
ifeq ($(CONFIG_DEBUG_SYMBOLS),y) |
@gustavonihei you are right. But grep for LDFLAGS! |
You do not need to change every flag provided to LDFLAGS. These ones I mentioned are the ones that are specific to the LD linker. There might be some others that may need to passed via |
Do we have any idea how the linker was interpreting these and what the effect was? |
@davids5 , please, see issue #3826 and my question on binutlis https://sourceware.org/pipermail/binutils/2021-June/116825.html |
@AlexanderVasiljev @Ouss4 - I will test this today. |
@v01d - any implication on the Cmake side? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The correct approach is described in comment #3836 (comment)
@gustavonihei -On a separate note for a future feature with LTO it would be great to get it working, I have seen it destroy working code when we needed the size saving. I did not dig into it as the time, nor do I want to have to just because of an NuttX upgrade. So please consider Kconfig control (with it off by default) |
@AlexanderVasiljev - we should either close this or update it to invoke the compiler - how would you like to proceed? |
Not really, cmake is much clearer about the distinction between compiler/linker flags so I did not encounter this issue. |
Yes, I have done a quick attempt at enabling the LTO build for ESP32, and I haven't assessed what exactly had been removed, but that's how far I've got. |
So far LTO has not proven to be Link Time Optimization but rather Limited Time Off :) |
@davids5 @gustavonihei OK, I will try to change LD for GCC in this pull request. |
A related comment could be that in fact CMake does not normally access |
I will start with armv7-m. |
Linker specific flags are used intensively in NXFLATLDFLAGS1 and NXFLATLDFLAGS2 macros. But I cannot find where they |
let's sqush all patch into one. |
sim:smp |
@masayuki2009 It have built successfully on my PC. Can you make clean? And reconfigure! Make.defs file should be relocated!
|
Yes. I tried git clean -fdx for both directories before rebuilding the target. |
@masayuki2009 Here are my steps Result
|
@masayuki2009 Is your apps on the recent commit? |
@masayuki2009 I remember that I encountered this error with -lgcc_s. It was when I tried to migrate ld to gcc for x86. It failed because of "-static" flag, which was intoduced by this commit aed9bcc#diff-2d60830f249881b0f33a243863a1f9fa2194e57d21faa126a49cd8e8c0f31c5b I gave up to migrate x86. Could you explain why -static is needed there? Doesn't your toolchain have -static flag by default? |
@AlexanderVasiljev
How can I confirm it?
|
@@ -83,7 +83,7 @@ endif | |||
CC = $(CROSSDEV)cc | |||
CXX = $(CROSSDEV)c++ | |||
CPP = $(CROSSDEV)cc -E -P -x c | |||
LD = $(CROSSDEV)ld | |||
LD = $(CROSSDEV)gcc |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is breaking our clang-based build. Our build fails like:
echo "LD: nuttx"
LD: nuttx
gcc -r -L"/nuttx/staging" -L board -o nuttx.rel up_head.o -Wl,--start-group -lsched -ldrivers -lboards -lc -lmm -larch -lapps -lnet -lfs -lbinfmt -lboard -Wl,--end-group
/bin/sh: 1: gcc: not found
make[1]: Leaving directory '/nuttx/arch/sim/src'
make[1]: *** [Makefile:314: nuttx] Error 127
make: *** [tools/Makefile.unix:422: nuttx] Error 2
The command '/bin/sh -c bear -v make -j $(grep -c ^processor /proc/cpuinfo) V=2' returned a non-zero code: 2
make: *** [process/Build.mk:39: builder] Error
Previously it was:
ld -r -L"/nuttx/staging" -L board -o nuttx.rel up_head.o --start-group -lsched -ldrivers -lboards -lc -lmm -larch -lapps -lnet -lfs -lbinfmt -lboard --end-group
Certainly I agree with the suggestion this change should have been widely advertised before merging.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
diff --git a/boards/sim/sim/sim/scripts/Make.defs b/boards/sim/sim/sim/scripts/Make.defs
index 65600e15ec..e4615f12f1 100644
--- a/boards/sim/sim/sim/scripts/Make.defs
+++ b/boards/sim/sim/sim/scripts/Make.defs
@@ -83,7 +83,7 @@ endif
CC = $(CROSSDEV)cc
CXX = $(CROSSDEV)c++
CPP = $(CROSSDEV)cc -E -P -x c
-LD = $(CROSSDEV)gcc
+LD = $(CROSSDEV)cc
ifeq ($(CONFIG_HOST_MACOS),y)
STRIP = $(CROSSDEV)strip
AR = $(TOPDIR)/tools/macar-rcs.sh
@lulingar Please, try applying this patch
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, it failed further down the build, like:
echo "LD: nuttx"
LD: nuttx
cc -r -L"/nuttx/staging" -L board -o nuttx.rel up_head.o --start-group -lsched -ldrivers -lboards -lc -lmm -larch -lapps -lnet -lfs -lbinfmt -lboard --end-group
clang: error: unsupported option '--start-group'
clang: error: unsupported option '--end-group'
make[1]: Leaving directory '/nuttx/arch/sim/src'
make[1]: *** [Makefile:314: nuttx] Error 1
make: *** [tools/Makefile.unix:422: nuttx] Error 2
But it seems #3904 fixes this, I'm testing it out.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It didn't fix it, in our clang-only build we don't have gcc. I'm testing some other ways to go about this. But this begs the question, why forcing this way the compiler to be gcc?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@lulingar Do you have any instructions how to config clang build? What OS do you use?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In my Ubuntu 21.04 machine, I've just successfully built sim:nsh
with clang. For forcing the clang
compiler since it's not symlinked to cc
, I've applied the following patch:
diff --git a/boards/sim/sim/sim/scripts/Make.defs b/boards/sim/sim/sim/scripts/Make.defs
index 067914aea2..0d2a861890 100644
--- a/boards/sim/sim/sim/scripts/Make.defs
+++ b/boards/sim/sim/sim/scripts/Make.defs
@@ -80,10 +80,10 @@ ifeq ($(CONFIG_SIM_M32),y)
ARCHCPUFLAGSXX += -m32
endif
-CC = $(CROSSDEV)cc
-CXX = $(CROSSDEV)c++
-CPP = $(CROSSDEV)cc -E -P -x c
-LD = $(CROSSDEV)gcc
+CC = $(CROSSDEV)clang
+CXX = $(CROSSDEV)clang++
+CPP = $(CROSSDEV)clang -E -P -x c
+LD = $(CROSSDEV)clang
ifeq ($(CONFIG_HOST_MACOS),y)
STRIP = $(CROSSDEV)strip
AR = $(TOPDIR)/tools/macar-rcs.sh
Also, I've successfully built sim:nsh
on macOS with clang as the default compiler.
@lulingar, from the command line you've posted we can see that --start-group
was incorrectly passed instead of -Wl,--start-group
. Have you performed a distclean
after you pulled the latest changes from upstream? Since the Makefiles from the board were changed, these are not automatically refreshed by make
because the NuttX build system makes a copy of them on the ./tools/configure.sh
step.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the indications. I have tested aliasing clang as gcc, after the merge of #3904, and it worked.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@masayuki2009 I have just installed Ubuntu 18.04. I can see the same error. I will investigate it. |
@@ -93,8 +93,8 @@ LDFLAGS += $(ARCHSCRIPT) $(EXTRALINKCMDS) | |||
|
|||
# Override in Make.defs if linker is not 'ld' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now misleading comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually this comment still applies, since the GCC driver will call LD to perform the linking process.
@xiaoxiang781216 , @davids5 , @AlexanderVasiljev This change is now described in the CWIKI Release Notes for the (future) 10.2 release: https://cwiki.apache.org/confluence/display/NUTTX/NuttX+10.2#ld-now-called-through-gcc Please proofread and feel free to make corrections. As this is a breaking change that happens when the Package Manager updates Binutils to 2.36.x, maybe it should be added to the website for anyone who encounters this problem pre-10.2-release? |
By the way, in places like this:
Why does the |
why do we need -nostartfiles -nodefaultlibs at all? besides, having cc as LD is very confusing. |
|
GCC treat -r the same way as LD.
|
Almost every board configuration has these options. It seems like to be a copypasta. But actualy LD doesn't process them. My first proposal was to change it to CFLAGS. But changing LD to GCC can give more options for linking optimization. So it was decided to take more effort. |
@masayuki2009 Please, try this patch boards/sim/sim/sim/scripts/Make.defs
|
@AlexanderVasiljev |
i'm not sure if i agree the decision. i submitted a revert. #3900 |
…of GCC It seems that those flags have always been gcc flags, and not ld flags. After decades of tolerating this, binutils 2.36.x no longer tolerates those flags but prints an error: arm-none-eabi-ld: Error: unable to disambiguate: -nostartfiles (did you mean --nostartfiles ?) See also apache/nuttx#3826 and the related apache/nuttx#3836 how this was solved in another project - I adopted that solution here 1:1 Change-Id: Id199e4d03d5aae07a347c98f47791f42c12008c6
Summary
The flags -nostartfiles -nodefaultlibs are not the flags of LD but are the flags of GCC. It will cause errors on new GNU ld 2.36.x.
Now GNU ld just doesn't recognize them.
New GNU ld version 2.36.x will treat this as error.
See issue #3826
This patch will use GCC as LD. So the main change is
This PR covers arm, avr, z80, or1k, misos, hc. All boards in the repository of these archs are corrected to be linked with GCC. At least they are compilable.
Impact
GCC will be used in linkage stage, so every LD flag should be prefixed with -Wl.
Every board should correct linker flags.
The common change for most boards is
Testing
custom board