Skip to contentSkip to conversation

toolchain-external: adjust logic for musl dynamic linker symlink

PATCH New Thomas Petazzoni 3 participants 3 messages Jun 24, 2017 – Jun 25, 2017

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 toolchain-external: adjust logic for musl dynamic linker symlink

This commit changes the strategy to know what symlink should be created for the musl dynamic linker. For now, we have in Buildroot the logic to decide what the symlink name should be. However, despite numerous fixes, there are still some cases that are missing, such as mips64el-n32, where the symlink is ld-musl-mipsn32el.so.1.

So, instead of trying to make Buildroot aware of all the possible names, simply get it from the external toolchain.

---
Changes since v1:
- As suggested by Yann E. Morin, build a sample program, and get its program interpreter with readelf, in order to find the program interpreter.

Note: I have tried to refactor this with the check_musl function in toolchain/helpers.mk, with the idea of introducing a function get_program_interpreter that would, given CC/READELF would return the program interpreter path. However, for the life of me, I couldn't get a make function invoking multiple shell commands to return a string.

---
 toolchain/toolchain-external/pkg-toolchain-external.mk | 17 +++--------------
 1 file changed, 3 insertions(+), 14 deletions(-)
toolchain/toolchain-external/pkg-toolchain-external.mk +3 -14
index 8269345..938f73d 100644
@@ -476,21 +476,10 @@ endef
 # of the dynamic library loader. We just need to create a symbolic
 # link to libc.so with the appropriate name.
 ifeq ($(BR2_TOOLCHAIN_EXTERNAL_MUSL),y)
-ifeq ($(BR2_i386),y)
-MUSL_ARCH = i386
-else ifeq ($(BR2_ARM_EABIHF),y)
-MUSL_ARCH = armhf
-else ifeq ($(BR2_mips):$(BR2_SOFT_FLOAT),y:y)
-MUSL_ARCH = mips-sf
-else ifeq ($(BR2_mipsel):$(BR2_SOFT_FLOAT),y:y)
-MUSL_ARCH = mipsel-sf
-else ifeq ($(BR2_sh),y)
-MUSL_ARCH = sh
-else
-MUSL_ARCH = $(ARCH)
-endif
 define TOOLCHAIN_EXTERNAL_MUSL_LD_LINK
-	ln -sf libc.so $(TARGET_DIR)/lib/ld-musl-$(MUSL_ARCH).so.1
+	$(Q)echo 'void main(void) {}' | $(TOOLCHAIN_EXTERNAL_CC) -x c -o $(BUILD_DIR)/.br-toolchain-test.tmp - >/dev/null 2>&1; \
+	PROGRAM_INTERPRETER=`$(TOOLCHAIN_EXTERNAL_READELF) -l $(BUILD_DIR)/.br-toolchain-test.tmp 2> /dev/null | sed -r -e '/.*Requesting program interpreter: (.+)\]$$/!d; s//\1/'` ; \
+	ln -sf libc.so $(TARGET_DIR)/$${PROGRAM_INTERPRETER}
 endef
 endif
 
Signature · 1 line
2.9.4
#1 lore raw

PATCH v2 1/1 toolchain-external: adjust logic for musl dynamic linker symlink

Quoted 10 lines · Thomas Petazzoni

This commit changes the strategy to know what symlink should be created for the musl dynamic linker. For now, we have in Buildroot the logic to decide what the symlink name should be. However, despite numerous fixes, there are still some cases that are missing, such as mips64el-n32, where the symlink is ld-musl-mipsn32el.so.1.

So, instead of trying to make Buildroot aware of all the possible names, simply get it from the external toolchain.

With some nits.

Quoted 11 lines

---
Changes since v1:
- As suggested by Yann E. Morin, build a sample program, and get its program interpreter with readelf, in order to find the program interpreter.

Note: I have tried to refactor this with the check_musl function in toolchain/helpers.mk, with the idea of introducing a function get_program_interpreter that would, given CC/READELF would return the program interpreter path. However, for the life of me, I couldn't get a make function invoking multiple shell commands to return a string.

Huh?

get_program_interpreter = $(shell \
echo 'void main(void) {}' | $(TOOLCHAIN_EXTERNAL_CC) -x c -o
$(BUILD_DIR)/.br-toolchain-test.tmp - >/dev/null 2>&1; \
$(TOOLCHAIN_EXTERNAL_READELF) -l $(BUILD_DIR)/.br-toolchain-test.tmp 2>
/dev/null | sed -r -e '/.*Requesting program interpreter: (.+)\]$$/!d; s//\1/')

$(warning $(get_program_interpreter))

toolchain/helpers.mk:496: /lib/ld-musl-i386.so.1

Quoted 19 lines
---
 toolchain/toolchain-external/pkg-toolchain-external.mk | 17 +++--------------
 1 file changed, 3 insertions(+), 14 deletions(-)
toolchain/toolchain-external/pkg-toolchain-external.mk +0 -7
index 8269345..938f73d 100644
@@ -476,21 +476,10 @@ endef
 # of the dynamic library loader. We just need to create a symbolic
 # link to libc.so with the appropriate name.
 ifeq ($(BR2_TOOLCHAIN_EXTERNAL_MUSL),y)
-ifeq ($(BR2_i386),y)
-MUSL_ARCH = i386
-else ifeq ($(BR2_ARM_EABIHF),y)
-MUSL_ARCH = armhf
-else ifeq ($(BR2_mips):$(BR2_SOFT_FLOAT),y:y)
-MUSL_ARCH = mips-sf
-else ifeq ($(BR2_mipsel):$(BR2_SOFT_FLOAT),y:y)

toolchain/toolchain-external/pkg-toolchain-external.mk endef · line 481

Quoted 10 lines
toolchain/toolchain-external/pkg-toolchain-external.mk +2 -7
-MUSL_ARCH = mipsel-sf
-else ifeq ($(BR2_sh),y)
-MUSL_ARCH = sh
-else
-MUSL_ARCH = $(ARCH)
-endif
 define TOOLCHAIN_EXTERNAL_MUSL_LD_LINK
-	ln -sf libc.so $(TARGET_DIR)/lib/ld-musl-$(MUSL_ARCH).so.1
+	$(Q)echo 'void main(void) {}' | $(TOOLCHAIN_EXTERNAL_CC) -x c -o $(BUILD_DIR)/.br-toolchain-test.tmp - >/dev/null 2>&1; \
+	PROGRAM_INTERPRETER=`$(TOOLCHAIN_EXTERNAL_READELF) -l $(BUILD_DIR)/.br-toolchain-test.tmp 2> /dev/null | sed -r -e '/.*Requesting program interpreter: (.+)\]$$/!d; s//\1/'` ; \

Perhaps split this ridiculously long line after the | ? Perhaps also split the
two sed expressions with two -e.

I personally prefer sed -n -e '/.../s//\1/p' but I don't care much.

Regards,
Arnout

Quoted 5 lines
toolchain/toolchain-external/pkg-toolchain-external.mk +1 -0
+	ln -sf libc.so $(TARGET_DIR)/$${PROGRAM_INTERPRETER}
 endef
 endif
 
Signature · 6 lines
Arnout Vandecappelle                          arnout at mind be
Senior Embedded Software Architect            +32-16-286500
Essensium/Mind                                http://www.mind.be
G.Geenslaan 9, 3001 Leuven, Belgium           BE 872 984 063 RPR Leuven
LinkedIn profile: http://www.linkedin.com/in/arnoutvandecappelle
GPG fingerprint:  7493 020B C7E3 8618 8DEC 222C 82EB F404 F9AC 0DDF
#2 lore raw

PATCH v2 1/1 toolchain-external: adjust logic for musl dynamic linker symlink

Thomas, All,

On 2017-06-24 23:49 +0200, Arnout Vandecappelle spake thusly:

Quoted 7 lines · Thomas Petazzoni
Quoted 6 lines

This commit changes the strategy to know what symlink should be created for the musl dynamic linker. For now, we have in Buildroot the logic to decide what the symlink name should be. However, despite numerous fixes, there are still some cases that are missing, such as mips64el-n32, where the symlink is ld-musl-mipsn32el.so.1.

Quoted 6 lines
Quoted 4 lines

So, instead of trying to make Buildroot aware of all the possible names, simply get it from the external toolchain.

Not from me, see below (the worst offender being the missing LC_ALL).

Quoted 22 lines

With some nits.

Quoted 11 lines

---
Changes since v1:
- As suggested by Yann E. Morin, build a sample program, and get its program interpreter with readelf, in order to find the program interpreter.

Note: I have tried to refactor this with the check_musl function in toolchain/helpers.mk, with the idea of introducing a function get_program_interpreter that would, given CC/READELF would return the program interpreter path. However, for the life of me, I couldn't get a make function invoking multiple shell commands to return a string.

Huh?

get_program_interpreter = $(shell \
echo 'void main(void) {}' | $(TOOLCHAIN_EXTERNAL_CC) -x c -o
$(BUILD_DIR)/.br-toolchain-test.tmp - >/dev/null 2>&1; \
$(TOOLCHAIN_EXTERNAL_READELF) -l $(BUILD_DIR)/.br-toolchain-test.tmp 2>
/dev/null | sed -r -e '/.*Requesting program interpreter: (.+)\]$$/!d; s//\1/')

Quoted 3 lines

$(warning $(get_program_interpreter))

toolchain/helpers.mk:496: /lib/ld-musl-i386.so.1

Yup, works for me too...

Although I prefer such long macros be defined rather than assigned.

Quoted 33 lines
Quoted 19 lines
---
 toolchain/toolchain-external/pkg-toolchain-external.mk | 17 +++--------------
 1 file changed, 3 insertions(+), 14 deletions(-)
toolchain/toolchain-external/pkg-toolchain-external.mk +0 -7
index 8269345..938f73d 100644
@@ -476,21 +476,10 @@ endef
 # of the dynamic library loader. We just need to create a symbolic
 # link to libc.so with the appropriate name.
 ifeq ($(BR2_TOOLCHAIN_EXTERNAL_MUSL),y)
-ifeq ($(BR2_i386),y)
-MUSL_ARCH = i386
-else ifeq ($(BR2_ARM_EABIHF),y)
-MUSL_ARCH = armhf
-else ifeq ($(BR2_mips):$(BR2_SOFT_FLOAT),y:y)
-MUSL_ARCH = mips-sf
-else ifeq ($(BR2_mipsel):$(BR2_SOFT_FLOAT),y:y)

toolchain/toolchain-external/pkg-toolchain-external.mk endef · line 481

Quoted 10 lines
toolchain/toolchain-external/pkg-toolchain-external.mk +2 -7
-MUSL_ARCH = mipsel-sf
-else ifeq ($(BR2_sh),y)
-MUSL_ARCH = sh
-else
-MUSL_ARCH = $(ARCH)
-endif
 define TOOLCHAIN_EXTERNAL_MUSL_LD_LINK
-	ln -sf libc.so $(TARGET_DIR)/lib/ld-musl-$(MUSL_ARCH).so.1
+	$(Q)echo 'void main(void) {}' | $(TOOLCHAIN_EXTERNAL_CC) -x c -o $(BUILD_DIR)/.br-toolchain-test.tmp - >/dev/null 2>&1; \
+	PROGRAM_INTERPRETER=`$(TOOLCHAIN_EXTERNAL_READELF) -l $(BUILD_DIR)/.br-toolchain-test.tmp 2> /dev/null | sed -r -e '/.*Requesting program interpreter: (.+)\]$$/!d; s//\1/'` ; \

Perhaps split this ridiculously long line after the | ? Perhaps also split the
two sed expressions with two -e.

Quoted 1 line

I personally prefer sed -n -e '/.../s//\1/p' but I don't care much.

Also:
- force LC_ALL=C when calling readelf, to avoid any translation,
- remove the generated file.

Regards,
Yann E. MORIN.

Quoted 16 lines

Regards,
Arnout

Quoted 5 lines
toolchain/toolchain-external/pkg-toolchain-external.mk +1 -0
+	ln -sf libc.so $(TARGET_DIR)/$${PROGRAM_INTERPRETER}
 endef
 endif
 
Signature · 6 lines
Arnout Vandecappelle                          arnout at mind be
Senior Embedded Software Architect            +32-16-286500
Essensium/Mind                                http://www.mind.be
G.Geenslaan 9, 3001 Leuven, Belgium           BE 872 984 063 RPR Leuven
LinkedIn profile: http://www.linkedin.com/in/arnoutvandecappelle
GPG fingerprint:  7493 020B C7E3 8618 8DEC 222C 82EB F404 F9AC 0DDF
Signature · 6 lines
.-----------------.--------------------.------------------.--------------------.
|  Yann E. MORIN  | Real-Time Embedded | /"\ ASCII RIBBON | Erics' conspiracy: |
| +33 662 376 056 | Software  Designer | \ / CAMPAIGN     |  ___               |
| +33 223 225 172 `------------.-------:  X  AGAINST      |  \e/  There is no  |
| http://ymorin.is-a-geek.org/ | _/*\_ | / \ HTML MAIL    |   v   conspiracy.  |
'------------------------------^-------^------------------^--------------------'