Skip to contentSkip to conversation

x86: avoid SORT_BY_INIT_PRIORITY with old GNU ld

PATCH Superseded Jan Beulich 2 participants 7 messages Mar 7, 2022

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86: avoid SORT_BY_INIT_PRIORITY with old GNU ld

Support for this construct was added in 2.22 only. Avoid the need to introduce logic to probe for linker script capabilities by (ab)using the probe for a command line option having appeared at about the same time.

Fixes 4b7fd8153ddf ("x86: fold sections in final binaries")

---
Obviously this doesn't take care of old LLVM ld yet, if any care is needed there in the first place.

xen/arch/x86/arch.mk +5 -0
@@ -70,6 +70,11 @@ ifeq ($(CONFIG_UBSAN),y)
 $(call cc-option-add,CFLAGS_UBSAN,CC,-fno-sanitize=alignment)
 endif
 
+# While not much better than going by raw GNU ld version, utilize that the
+# feature we're after has appeared in the same release as the
+# --print-output-format command line option.
+AFLAGS-$(call ld-option,--print-output-format) += -DHAVE_LD_SORT_BY_INIT_PRIORITY
+
 ifneq ($(CONFIG_PV_SHIM_EXCLUSIVE),y)
 
 efi-check := arch/x86/efi/check
xen/arch/x86/xen.lds.S +4 -0
@@ -30,6 +30,10 @@ ENTRY(start_pa)
 # define SECTION_ALIGN PAGE_SIZE
 #endif
 
+#ifndef HAVE_LD_SORT_BY_INIT_PRIORITY
+# define SORT_BY_INIT_PRIORITY SORT
+#endif
+
 OUTPUT_FORMAT(FORMAT, FORMAT, FORMAT)
 
 OUTPUT_ARCH(i386:x86-64)
#1 lore raw
Quoted 13 lines · Jan Beulich

Support for this construct was added in 2.22 only. Avoid the need to introduce logic to probe for linker script capabilities by (ab)using the probe for a command line option having appeared at about the same time.

Fixes 4b7fd8153ddf ("x86: fold sections in final binaries")

---
Obviously this doesn't take care of old LLVM ld yet, if any care is needed there in the first place.

xen/arch/x86/arch.mk ifeq ($(CONFIG_UBSAN),y) · line 76

Quoted 7 lines
xen/arch/x86/arch.mk +4 -0
@@ -70,6 +70,11 @@ ifeq ($(CONFIG_UBSAN),y)
 $(call cc-option-add,CFLAGS_UBSAN,CC,-fno-sanitize=alignment)
 endif
 
+# While not much better than going by raw GNU ld version, utilize that the
+# feature we're after has appeared in the same release as the
+# --print-output-format command line option.
+AFLAGS-$(call ld-option,--print-output-format) += -DHAVE_LD_SORT_BY_INIT_PRIORITY

LLVM ld doesn't have --print-output-format:

% ld --print-output-format
ld: error: unknown argument '--print-output-format'

So it would be always defaulting to SORT. I'm not sure what to recommend.

Regards, Roger.

#2 lore raw
Quoted 24 lines · Roger Pau Monné
Quoted 13 lines · Jan Beulich

Support for this construct was added in 2.22 only. Avoid the need to introduce logic to probe for linker script capabilities by (ab)using the probe for a command line option having appeared at about the same time.

Fixes 4b7fd8153ddf ("x86: fold sections in final binaries")

---
Obviously this doesn't take care of old LLVM ld yet, if any care is needed there in the first place.

xen/arch/x86/arch.mk ifeq ($(CONFIG_UBSAN),y) · line 76

Quoted 7 lines
xen/arch/x86/arch.mk +4 -0
@@ -70,6 +70,11 @@ ifeq ($(CONFIG_UBSAN),y)
 $(call cc-option-add,CFLAGS_UBSAN,CC,-fno-sanitize=alignment)
 endif
 
+# While not much better than going by raw GNU ld version, utilize that the
+# feature we're after has appeared in the same release as the
+# --print-output-format command line option.
+AFLAGS-$(call ld-option,--print-output-format) += -DHAVE_LD_SORT_BY_INIT_PRIORITY

LLVM ld doesn't have --print-output-format:

Quoted 5 lines

% ld --print-output-format
ld: error: unknown argument '--print-output-format'

So it would be always defaulting to SORT. I'm not sure what to recommend.

Do you know if all versions we support know of SORT_BY_INIT_PRIORITY?

Jan

#3 lore raw
Quoted 25 lines · Jan Beulich
Quoted 24 lines · Roger Pau Monné
Quoted 13 lines · Jan Beulich

Support for this construct was added in 2.22 only. Avoid the need to introduce logic to probe for linker script capabilities by (ab)using the probe for a command line option having appeared at about the same time.

Fixes 4b7fd8153ddf ("x86: fold sections in final binaries")

---
Obviously this doesn't take care of old LLVM ld yet, if any care is needed there in the first place.

xen/arch/x86/arch.mk ifeq ($(CONFIG_UBSAN),y) · line 76

Quoted 7 lines
xen/arch/x86/arch.mk +4 -0
@@ -70,6 +70,11 @@ ifeq ($(CONFIG_UBSAN),y)
 $(call cc-option-add,CFLAGS_UBSAN,CC,-fno-sanitize=alignment)
 endif
 
+# While not much better than going by raw GNU ld version, utilize that the
+# feature we're after has appeared in the same release as the
+# --print-output-format command line option.
+AFLAGS-$(call ld-option,--print-output-format) += -DHAVE_LD_SORT_BY_INIT_PRIORITY

LLVM ld doesn't have --print-output-format:

Quoted 7 lines
Quoted 5 lines

% ld --print-output-format
ld: error: unknown argument '--print-output-format'

So it would be always defaulting to SORT. I'm not sure what to recommend.

Do you know if all versions we support know of SORT_BY_INIT_PRIORITY?

Hm, I don't think we can assume that we support LLVM LD in 3.5. I'm not even sure if LLVM 3.5 had LLD in the first place.

The first version that we care about and that we test is LLVM LD 6, anything below that version is of unknown state.

I've tested you change with SORT_BY_INIT_PRIORITY on it and it seems to be fine. This was on FreeBSD 12.3 version of LLD, not sure how many changes have been backported from newer versions there.

Thanks, Roger.

#4 lore raw
Quoted 37 lines · Roger Pau Monné
Quoted 25 lines · Jan Beulich
Quoted 24 lines · Roger Pau Monné
Quoted 13 lines · Jan Beulich

Support for this construct was added in 2.22 only. Avoid the need to introduce logic to probe for linker script capabilities by (ab)using the probe for a command line option having appeared at about the same time.

Fixes 4b7fd8153ddf ("x86: fold sections in final binaries")

---
Obviously this doesn't take care of old LLVM ld yet, if any care is needed there in the first place.

xen/arch/x86/arch.mk ifeq ($(CONFIG_UBSAN),y) · line 76

Quoted 7 lines
xen/arch/x86/arch.mk +4 -0
@@ -70,6 +70,11 @@ ifeq ($(CONFIG_UBSAN),y)
 $(call cc-option-add,CFLAGS_UBSAN,CC,-fno-sanitize=alignment)
 endif
 
+# While not much better than going by raw GNU ld version, utilize that the
+# feature we're after has appeared in the same release as the
+# --print-output-format command line option.
+AFLAGS-$(call ld-option,--print-output-format) += -DHAVE_LD_SORT_BY_INIT_PRIORITY

LLVM ld doesn't have --print-output-format:

Quoted 7 lines
Quoted 5 lines

% ld --print-output-format
ld: error: unknown argument '--print-output-format'

So it would be always defaulting to SORT. I'm not sure what to recommend.

Do you know if all versions we support know of SORT_BY_INIT_PRIORITY?

Hm, I don't think we can assume that we support LLVM LD in 3.5. I'm not even sure if LLVM 3.5 had LLD in the first place.

Quoted 6 lines

The first version that we care about and that we test is LLVM LD 6, anything below that version is of unknown state.

I've tested you change with SORT_BY_INIT_PRIORITY on it and it seems to be fine. This was on FreeBSD 12.3 version of LLD, not sure how many changes have been backported from newer versions there.

I'm inclined to suggest then that we unconditionally enable use of this, in the hope that we'll never see a bug report. But of course this then again gets me into the business of needing to determine the which ld variant we're working with. Looks like I won't be able to escape this anymore ...

Jan

#5 lore raw
Quoted 41 lines · Jan Beulich
Quoted 40 lines · Roger Pau Monné
Quoted 25 lines · Jan Beulich
Quoted 24 lines · Roger Pau Monné
Quoted 13 lines · Jan Beulich

Support for this construct was added in 2.22 only. Avoid the need to introduce logic to probe for linker script capabilities by (ab)using the probe for a command line option having appeared at about the same time.

Fixes 4b7fd8153ddf ("x86: fold sections in final binaries")

---
Obviously this doesn't take care of old LLVM ld yet, if any care is needed there in the first place.

xen/arch/x86/arch.mk ifeq ($(CONFIG_UBSAN),y) · line 76

Quoted 7 lines
xen/arch/x86/arch.mk +4 -0
@@ -70,6 +70,11 @@ ifeq ($(CONFIG_UBSAN),y)
 $(call cc-option-add,CFLAGS_UBSAN,CC,-fno-sanitize=alignment)
 endif
 
+# While not much better than going by raw GNU ld version, utilize that the
+# feature we're after has appeared in the same release as the
+# --print-output-format command line option.
+AFLAGS-$(call ld-option,--print-output-format) += -DHAVE_LD_SORT_BY_INIT_PRIORITY

LLVM ld doesn't have --print-output-format:

Quoted 7 lines
Quoted 5 lines

% ld --print-output-format
ld: error: unknown argument '--print-output-format'

So it would be always defaulting to SORT. I'm not sure what to recommend.

Do you know if all versions we support know of SORT_BY_INIT_PRIORITY?

Hm, I don't think we can assume that we support LLVM LD in 3.5. I'm not even sure if LLVM 3.5 had LLD in the first place.

The first version that we care about and that we test is LLVM LD 6, anything below that version is of unknown state.

Quoted 6 lines
Quoted 3 lines

I've tested you change with SORT_BY_INIT_PRIORITY on it and it seems to be fine. This was on FreeBSD 12.3 version of LLD, not sure how many changes have been backported from newer versions there.

I'm inclined to suggest then that we unconditionally enable use of this, in the hope that we'll never see a bug report.

It's unlikely for anyone to try to use LLD < 6, as that wasn't considered capable of linking the FreeBSD kernel. In fact I should likely suggest an update to the README file for comments, as with my FreeBSD hat I only care about LLVM >= 6.

Quoted 4 lines

But of course
this then again gets me into the business of needing to determine the which ld variant we're working with. Looks like I won't be able to escape this anymore ...

I assume bumping binutils minimum version to 2.22 is not a viable option?

Thanks, Roger.

#6 lore raw
Quoted 2 lines · Roger Pau Monné

I assume bumping binutils minimum version to 2.22 is not a viable option?

It's an option, but is also part of the bigger area of determining what the baseline tool chain requirements should be established from.

Jan