toolchain: support mismatched merged usr
PATCH 1/2 toolchain: support mismatched merged usr
Look at the case where the source toolchain has non-merged usr, yet the target will have merged usr.
sysroot/lib/ld-musl-x86_64.so.1 -> ../usr/lib/libc.so sysroot/usr/lib/libc.so
What happens is that buildroot copies the ld-*so* symlink into usr/lib, at which point it becomes broken.
We now detect these broken symlinks, then try to find the target binary in the library directories and fix the link.
Fix the case where the lib directory is a symlink, and no ld-*so* is installed by adding -H to find.
Also use cp -t instead of some rarely used xargs tricks.
--- toolchain/helpers.mk | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-)
toolchain/helpers.mk +11 -2
@@ -135,8 +135,17 @@ copy_toolchain_sysroot = \ $(call simplify_symlink,$$i,$(STAGING_DIR)) ; \ done ; \ fi ; \ - if [[ ! $$(find $(STAGING_DIR)/lib -name 'ld*.so.*' -print -quit) ]]; then \ - find $${ARCH_SYSROOT_DIR}/lib -name 'ld*.so.*' -print0 | xargs -0 -I % cp % $(STAGING_DIR)/lib/; \ + for i in $$(find -H $(STAGING_DIR)/lib -name 'ld*.so.*' -xtype l); do \ + LINKTARGET=`readlink $$i`; \ + rm $$i; \ + NEWLINKTARGET=$$(find -H $(STAGING_DIR)/$${ARCH_LIB_DIR} $(STAGING_DIR)/lib $(STAGING_DIR)/usr/$${ARCH_LIB_DIR} $(STAGING_DIR)/usr/lib -name "`basename $${LINKTARGET}`" -print -quit); \ + if [ -n "$${NEWLINKTARGET}" -a -e "$${NEWLINKTARGET}" ]; then \ + ln -sr $${NEWLINKTARGET} $$i; \ + echo "Symlinking $$i -> `readlink $$i`" ; \ + fi; \ + done; \ + if [[ ! $$(find -H $(STAGING_DIR)/lib -name 'ld*.so.*' -print -quit) ]]; then \ + find $${ARCH_SYSROOT_DIR}/lib -name 'ld*.so.*' -print0 | xargs -0 cp -t $(STAGING_DIR)/lib/; \ fi ; \ if [ `readlink -f $${SYSROOT_DIR}` != `readlink -f $${ARCH_SYSROOT_DIR}` ] ; then \ if [ ! -d $${ARCH_SYSROOT_DIR}/usr/include ] ; then \
Signature · 6 lines
2.34.1 _______________________________________________ buildroot mailing list [email protected] https://lists.buildroot.org/mailman/listinfo/buildroot
PATCH 2/2 toolchain: prevent infinite loop in copy_toolchain_lib_root
This can happen when there are broken symlinks.
--- toolchain/helpers.mk | 1 + 1 file changed, 1 insertion(+)
toolchain/helpers.mk +1 -0
@@ -20,6 +20,7 @@ copy_toolchain_lib_root = \ if test -h $${LIBPATH} ; then \ cp -d $${LIBPATH} $(TARGET_DIR)/$${DESTDIR}/$${LIBNAME}; \ LIBPATH="`readlink -f $${LIBPATH}`"; \ + [ -n "$${LIBPATH}" ] || break; \ elif test -f $${LIBPATH}; then \ $(INSTALL) -D -m0755 $${LIBPATH} $(TARGET_DIR)/$${DESTDIR}/$${LIBNAME}; \ break ; \
Signature · 6 lines
2.34.1 _______________________________________________ buildroot mailing list [email protected] https://lists.buildroot.org/mailman/listinfo/buildroot
Re: [Buildroot] [PATCH 2/2] toolchain: prevent infinite loop in copy_toolchain_lib_root
Quoted 12 lines · Norbert Lange
This can happen when there are broken symlinks.
Signed-off-by Norbert Lange <[email protected]>--- toolchain/helpers.mk | 1 + 1 file changed, 1 insertion(+)toolchain/helpers.mk +0
-0
toolchain/helpers.mk copy_toolchain_lib_root = \ · line 23
Quoted 4 lines
toolchain/helpers.mk +1
-0@@ -20,6 +20,7 @@ copy_toolchain_lib_root = \ if test -h $${LIBPATH} ; then \ cp -d $${LIBPATH} $(TARGET_DIR)/$${DESTDIR}/$${LIBNAME}; \ LIBPATH="`readlink -f $${LIBPATH}`"; \ + [ -n "$${LIBPATH}" ] || break; \
I again don't understand how this can happen. LIBPATH exists and is a symlink
(tested a few lines above), so readlink will always return the contents of that symlink. I don't think that can ever be empty, but with the -f (full path) option it can definitely not be empty: readlink doesn't care about broken symlinks, and even if it has too many .. components it will just print something based on /.
Can you give an example of such a broken symlink where this can happen?
Regards,
Arnout
toolchain/helpers.mk copy_toolchain_lib_root = \ · line 26
Quoted 3 lines
toolchain/helpers.mk +0
-0elif test -f $${LIBPATH}; then \ $(INSTALL) -D -m0755 $${LIBPATH} $(TARGET_DIR)/$${DESTDIR}/$${LIBNAME}; \ break ; \
_______________________________________________
buildroot mailing list
[email protected]
https://lists.buildroot.org/mailman/listinfo/buildroot
Quoted 5 lines · Norbert Lange
Look at the case where the source toolchain has non-merged usr, yet the target will have merged usr.
sysroot/lib/ld-musl-x86_64.so.1 -> ../usr/lib/libc.so sysroot/usr/lib/libc.so
Where do you get an external toolchain with such a weird layout? Normally libc
is in sysroot/lib and ld-musl*.so.1 is a symlink to either /lib/libc.so or ../lib/libc.so.
Regards,
Arnout
Quoted 31 lines
What happens is that buildroot copies the ld-*so* symlink into usr/lib, at which point it becomes broken.
We now detect these broken symlinks, then try to find the target binary in the library directories and fix the link.
Fix the case where the lib directory is a symlink, and no ld-*so* is installed by adding -H to find.
Also use
cp -tinstead of some rarely used xargs tricks.Signed-off-by Norbert Lange <[email protected]>--- toolchain/helpers.mk | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-)toolchain/helpers.mk +4
-2@@ -135,8 +135,17 @@ copy_toolchain_sysroot = \ $(call simplify_symlink,$$i,$(STAGING_DIR)) ; \ done ; \ fi ; \ - if [[ ! $$(find $(STAGING_DIR)/lib -name 'ld*.so.*' -print -quit) ]]; then \ - find $${ARCH_SYSROOT_DIR}/lib -name 'ld*.so.*' -print0 | xargs -0 -I % cp % $(STAGING_DIR)/lib/; \ + for i in $$(find -H $(STAGING_DIR)/lib -name 'ld*.so.*' -xtype l); do \ + LINKTARGET=`readlink $$i`; \ + rm $$i; \ + NEWLINKTARGET=$$(find -H $(STAGING_DIR)/$${ARCH_LIB_DIR} $(STAGING_DIR)/lib $(STAGING_DIR)/usr/$${ARCH_LIB_DIR} $(STAGING_DIR)/usr/lib -name "`basename $${LINKTARGET}`" -print -quit); \
toolchain/helpers.mk copy_toolchain_sysroot = \ · line 151
Quoted 10 lines
toolchain/helpers.mk +7
-0+ if [ -n "$${NEWLINKTARGET}" -a -e "$${NEWLINKTARGET}" ]; then \ + ln -sr $${NEWLINKTARGET} $$i; \ + echo "Symlinking $$i -> `readlink $$i`" ; \ + fi; \ + done; \ + if [[ ! $$(find -H $(STAGING_DIR)/lib -name 'ld*.so.*' -print -quit) ]]; then \ + find $${ARCH_SYSROOT_DIR}/lib -name 'ld*.so.*' -print0 | xargs -0 cp -t $(STAGING_DIR)/lib/; \ fi ; \ if [ `readlink -f $${SYSROOT_DIR}` != `readlink -f $${ARCH_SYSROOT_DIR}` ] ; then \ if [ ! -d $${ARCH_SYSROOT_DIR}/usr/include ] ; then \
_______________________________________________
buildroot mailing list
[email protected]
https://lists.buildroot.org/mailman/listinfo/buildroot
Am Sa., 5. März 2022 um 16:09 Uhr schrieb Arnout Vandecappelle <[email protected]>:
Quoted 10 lines · Norbert Lange
Quoted 5 lines
Look at the case where the source toolchain has non-merged usr, yet the target will have merged usr.
sysroot/lib/ld-musl-x86_64.so.1 -> ../usr/lib/libc.so sysroot/usr/lib/libc.so
Quoted 8 lines
Where do you get an external toolchain with such a weird layout? Normally libc
is in sysroot/lib and ld-musl*.so.1 is a symlink to either/lib/libc.soor ../lib/libc.so.Regards,
Arnout
the toolchain is built with crosstool-ng
Quoted 33 lines
Quoted 33 lines
What happens is that buildroot copies the ld-*so* symlink into usr/lib, at which point it becomes broken.
We now detect these broken symlinks, then try to find the target binary in the library directories and fix the link.
Fix the case where the lib directory is a symlink, and no ld-*so* is installed by adding -H to find.
Also use
cp -tinstead of some rarely used xargs tricks.Signed-off-by Norbert Lange <[email protected]>--- toolchain/helpers.mk | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-)toolchain/helpers.mk +6
-2@@ -135,8 +135,17 @@ copy_toolchain_sysroot = \ $(call simplify_symlink,$$i,$(STAGING_DIR)) ; \ done ; \ fi ; \ - if [[ ! $$(find $(STAGING_DIR)/lib -name 'ld*.so.*' -print -quit) ]]; then \ - find $${ARCH_SYSROOT_DIR}/lib -name 'ld*.so.*' -print0 | xargs -0 -I % cp % $(STAGING_DIR)/lib/; \ + for i in $$(find -H $(STAGING_DIR)/lib -name 'ld*.so.*' -xtype l); do \ + LINKTARGET=`readlink $$i`; \ + rm $$i; \ + NEWLINKTARGET=$$(find -H $(STAGING_DIR)/$${ARCH_LIB_DIR} $(STAGING_DIR)/lib $(STAGING_DIR)/usr/$${ARCH_LIB_DIR} $(STAGING_DIR)/usr/lib -name "`basename $${LINKTARGET}`" -print -quit); \ + if [ -n "$${NEWLINKTARGET}" -a -e "$${NEWLINKTARGET}" ]; then \ + ln -sr $${NEWLINKTARGET} $$i; \
Quoted 8 lines
toolchain/helpers.mk copy_toolchain_sysroot = \ · line 151
Quoted 8 lines
toolchain/helpers.mk +5
-0+ echo "Symlinking $$i -> `readlink $$i`" ; \ + fi; \ + done; \ + if [[ ! $$(find -H $(STAGING_DIR)/lib -name 'ld*.so.*' -print -quit) ]]; then \ + find $${ARCH_SYSROOT_DIR}/lib -name 'ld*.so.*' -print0 | xargs -0 cp -t $(STAGING_DIR)/lib/; \ fi ; \ if [ `readlink -f $${SYSROOT_DIR}` != `readlink -f $${ARCH_SYSROOT_DIR}` ] ; then \ if [ ! -d $${ARCH_SYSROOT_DIR}/usr/include ] ; then \
_______________________________________________
buildroot mailing list
[email protected]
https://lists.buildroot.org/mailman/listinfo/buildroot
Hi Norbert,
Sorry that this has been laying around so long without reaction. The thing is
that this external toolchain sysroot stuff is incredibly fragile and even worse, difficult to understand. So there isn't much motivation to accept patches. We probably should have some tests for it...
Quoted 6 lines · Norbert Lange
Look at the case where the source toolchain has non-merged usr, yet the target will have merged usr.
sysroot/lib/ld-musl-x86_64.so.1 -> ../usr/lib/libc.so sysroot/usr/lib/libc.so
Quoted 8 lines
What happens is that buildroot copies the ld-*so* symlink into usr/lib, at which point it becomes broken.
We now detect these broken symlinks, then try to find the target binary in the library directories and fix the link.
Fix the case where the lib directory is a symlink, and no ld-*so* is installed by adding -H to find.
I don't understand what's the issue there. $(STAGING_DIR)/lib is always a
symlink in merged usr, and never a symlink in non-merged usr. Also, the -H parameter has no effect on the paths supplied in the command line (i.e. $(STAGING_DIR)/lib itself), only on the symlinks within it. So I don't understand what you're trying to fix here. Maybe you have a tuple->. symlink inside the lib directory? But even that won't be followed by find because it sees that it's a directory it already encountered. Or maybe it's a tuple->../usr/lib/tuple symlink? That also won't be followed by find because it's a broken symlink.
So, can you explain better (in the commit message) in which situation this is
needed?
Quoted 1 line
Also use
cp -tinstead of some rarely used xargs tricks.
That's pretty much in the eye of the beholder. I regularly use xargs -I, I
never used cp -t. We use neither of these constructs anywhere else in Buildroot. So if anything, it should probably be changed into find -exec which we do use elsewhere (though I honestly find it much more annoying to use than xargs -I).
Anyway, such an unrelated change should be a separate patch, so we can easily
skip it if we don't agree (or apply it if we don't agree with the rest).
toolchain/helpers.mk copy_toolchain_sysroot = \
Quoted 11 lines
Signed-off-by Norbert Lange <[email protected]>--- toolchain/helpers.mk | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-)toolchain/helpers.mk +0
-0@@ -135,8 +135,17 @@ copy_toolchain_sysroot = \
There's a huge comment above copy_toolchain_sysroot that explains in detail
what it does. It should really be interspersed with the code itself, but due to make limitations that's not possible. Anyway, that comment should be extended with an explanation of the why and the how of the new block.
toolchain/helpers.mk copy_toolchain_sysroot = \ · line 138
Quoted 6 lines
toolchain/helpers.mk +1
-2$(call simplify_symlink,$$i,$(STAGING_DIR)) ; \ done ; \ fi ; \ - if [[ ! $$(find $(STAGING_DIR)/lib -name 'ld*.so.*' -print -quit) ]]; then \ - find $${ARCH_SYSROOT_DIR}/lib -name 'ld*.so.*' -print0 | xargs -0 -I % cp % $(STAGING_DIR)/lib/; \ + for i in $$(find -H $(STAGING_DIR)/lib -name 'ld*.so.*' -xtype l); do \
Should there ever be more than one? IOW shouldn't there be a -quit?
Also, we usually use backticks instead of $$()
toolchain/helpers.mk copy_toolchain_sysroot = \ · line 141
Quoted 3 lines
toolchain/helpers.mk +3
-0+ LINKTARGET=`readlink $$i`; \ + rm $$i; \ + NEWLINKTARGET=$$(find -H $(STAGING_DIR)/$${ARCH_LIB_DIR} $(STAGING_DIR)/lib $(STAGING_DIR)/usr/$${ARCH_LIB_DIR} $(STAGING_DIR)/usr/lib -name "`basename $${LINKTARGET}`" -print -quit); \
What you actually want here is to take the first one that matches, right?
Also, it should always be in one of those directories themselves, and not in one of the subdirectories I think? In that case, I think it's more clear if it's a loop rather than find. So something like:
for libdir in $(STAGING_DIR)/$${ARCH_LIB_DIR} $(STAGING_DIR)/lib
$(STAGING_DIR)/usr/$${ARCH_LIB_DIR} $(STAGING_DIR)/usr/lib; do \
NEWLINKTARGET="$${libdir}/`basename $${LINKTARGET}`"; \
if [ -n "$${NEWLINKTARGET}" -a -e "$${NEWLINKTARGET}" ]; then \
echo "Symlinking $$i -> `readlink $$i`" ; \
ln -sr $${NEWLINKTARGET} $$i; \
break;
fi; \
done; \(this was just some quick coding, I probably missed a lot of things).
toolchain/helpers.mk copy_toolchain_sysroot = \ · line 142
Quoted 1 line
toolchain/helpers.mk +1
-0+ if [ -n "$${NEWLINKTARGET}" -a -e "$${NEWLINKTARGET}" ]; then \
If this is not true, was it a good idea to delete the symlink? Or maybe we
should even error out in that case?
toolchain/helpers.mk copy_toolchain_sysroot = \ · line 148
Quoted 6 lines
toolchain/helpers.mk +6
-0+ ln -sr $${NEWLINKTARGET} $$i; \ + echo "Symlinking $$i -> `readlink $$i`" ; \ + fi; \ + done; \ + if [[ ! $$(find -H $(STAGING_DIR)/lib -name 'ld*.so.*' -print -quit) ]]; then \ + find $${ARCH_SYSROOT_DIR}/lib -name 'ld*.so.*' -print0 | xargs -0 cp -t $(STAGING_DIR)/lib/; \
I wonder if this bit couldn't be merged with the above (skipping the -xtype in
the find of course).
Regards,
Arnout
toolchain/helpers.mk copy_toolchain_sysroot = \ · line 151
Quoted 3 lines
toolchain/helpers.mk +0
-0fi ; \ if [ `readlink -f $${SYSROOT_DIR}` != `readlink -f $${ARCH_SYSROOT_DIR}` ] ; then \ if [ ! -d $${ARCH_SYSROOT_DIR}/usr/include ] ; then \
_______________________________________________
buildroot mailing list
[email protected]
https://lists.buildroot.org/mailman/listinfo/buildroot