[PATCH v1] kbuild: Add KBUILD_VMLINUX_LIBS_PRELINK

Tiezhu Yang posted 1 patch 1 week, 5 days ago
Makefile                | 1 +
scripts/link-vmlinux.sh | 5 ++---
2 files changed, 3 insertions(+), 3 deletions(-)
[PATCH v1] kbuild: Add KBUILD_VMLINUX_LIBS_PRELINK
Posted by Tiezhu Yang 1 week, 5 days ago
In order to only link libstub to the final vmlinux, it can not use the
current KBUILD_VMLINUX_LIBS, just add KBUILD_VMLINUX_LIBS_PRELINK. This
is preparation for later patch, no functionality change.

Link: https://lore.kernel.org/lkml/pq4h7jgndnt6p45lj4kgubxjd5gidfetugcuf5rcxzxxanzetd@6rrlpjnjsmuy/
Suggested-by: Josh Poimboeuf <jpoimboe@kernel.org>
Signed-off-by: Tiezhu Yang <yangtiezhu@loongson.cn>
---
There is a long discussion in the previous patch:
https://lore.kernel.org/lkml/20250928085506.4471-1-yangtiezhu@loongson.cn/

This version is based on 6.18-rc6, split the generic parts out
into a separate patch to avoid merge conflicts, the other parts
will send out after the merge window.

 Makefile                | 1 +
 scripts/link-vmlinux.sh | 5 ++---
 2 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/Makefile b/Makefile
index d763c2c75cdb..69485f47b794 100644
--- a/Makefile
+++ b/Makefile
@@ -1199,6 +1199,7 @@ KBUILD_VMLINUX_OBJS := built-in.a $(patsubst %/, %/lib.a, $(filter %/, $(libs-y)
 KBUILD_VMLINUX_LIBS := $(filter-out %/, $(libs-y))
 
 export KBUILD_VMLINUX_LIBS
+export KBUILD_VMLINUX_LIBS_PRELINK
 export KBUILD_LDS          := arch/$(SRCARCH)/kernel/vmlinux.lds
 
 ifdef CONFIG_TRIM_UNUSED_KSYMS
diff --git a/scripts/link-vmlinux.sh b/scripts/link-vmlinux.sh
index 433849ff7529..e72d3254b93f 100755
--- a/scripts/link-vmlinux.sh
+++ b/scripts/link-vmlinux.sh
@@ -61,12 +61,11 @@ vmlinux_link()
 	shift
 
 	if is_enabled CONFIG_LTO_CLANG || is_enabled CONFIG_X86_KERNEL_IBT; then
-		# Use vmlinux.o instead of performing the slow LTO link again.
 		objs=vmlinux.o
-		libs=
+		libs="${KBUILD_VMLINUX_LIBS_PRELINK}"
 	else
 		objs=vmlinux.a
-		libs="${KBUILD_VMLINUX_LIBS}"
+		libs="${KBUILD_VMLINUX_LIBS} ${KBUILD_VMLINUX_LIBS_PRELINK}"
 	fi
 
 	if is_enabled CONFIG_GENERIC_BUILTIN_DTB; then
-- 
2.42.0
Re: [PATCH v1] kbuild: Add KBUILD_VMLINUX_LIBS_PRELINK
Posted by Nathan Chancellor 1 week, 3 days ago
On Wed, Nov 19, 2025 at 12:27:08PM +0800, Tiezhu Yang wrote:
...
> index 433849ff7529..e72d3254b93f 100755
> --- a/scripts/link-vmlinux.sh
> +++ b/scripts/link-vmlinux.sh
> @@ -61,12 +61,11 @@ vmlinux_link()
>  	shift
>  
>  	if is_enabled CONFIG_LTO_CLANG || is_enabled CONFIG_X86_KERNEL_IBT; then
> -		# Use vmlinux.o instead of performing the slow LTO link again.

Why is this comment getting removed?

>  		objs=vmlinux.o
> -		libs=
> +		libs="${KBUILD_VMLINUX_LIBS_PRELINK}"
>  	else
>  		objs=vmlinux.a
> -		libs="${KBUILD_VMLINUX_LIBS}"
> +		libs="${KBUILD_VMLINUX_LIBS} ${KBUILD_VMLINUX_LIBS_PRELINK}"
>  	fi
>  
>  	if is_enabled CONFIG_GENERIC_BUILTIN_DTB; then
> -- 
> 2.42.0
>
Re: [PATCH v1] kbuild: Add KBUILD_VMLINUX_LIBS_PRELINK
Posted by Tiezhu Yang 1 week, 2 days ago
On 11/22/25 09:35, Nathan Chancellor wrote:
> On Wed, Nov 19, 2025 at 12:27:08PM +0800, Tiezhu Yang wrote:
> ...
>> index 433849ff7529..e72d3254b93f 100755
>> --- a/scripts/link-vmlinux.sh
>> +++ b/scripts/link-vmlinux.sh
>> @@ -61,12 +61,11 @@ vmlinux_link()
>>   	shift
>>   
>>   	if is_enabled CONFIG_LTO_CLANG || is_enabled CONFIG_X86_KERNEL_IBT; then
>> -		# Use vmlinux.o instead of performing the slow LTO link again.
> 
> Why is this comment getting removed?

When using KBUILD_VMLINUX_LIBS_PRELINK or KBUILD_VMLINUX_LIBS_FINAL,
drivers/firmware/efi/libstub/lib.a needs to be linked to the final
vmlinux with vmlinux.o, so I thought the above comment is not proper,
it I misunderstood it, I will keep the comment as is.

>>   		objs=vmlinux.o
>> -		libs=
>> +		libs="${KBUILD_VMLINUX_LIBS_PRELINK}"
>>   	else
>>   		objs=vmlinux.a
>> -		libs="${KBUILD_VMLINUX_LIBS}"
>> +		libs="${KBUILD_VMLINUX_LIBS} ${KBUILD_VMLINUX_LIBS_PRELINK}"
>>   	fi
>>   
>>   	if is_enabled CONFIG_GENERIC_BUILTIN_DTB; then

FYI, the previous patch is still under discussion yesterday [1],
I do not know whether this patch is necessary and what is the next
step [2].

[1] 
https://lore.kernel.org/loongarch/CAAhV-H4AasfFet_Gi_mVyte3RPMH3qBS73dBfF-=Gd7HJ6ZPEw@mail.gmail.com/
[2] 
https://lore.kernel.org/loongarch/39617a3e-c476-abac-8425-bbcece769cdb@loongson.cn/

Thanks,
Tiezhu
Re: [PATCH v1] kbuild: Add KBUILD_VMLINUX_LIBS_PRELINK
Posted by Nathan Chancellor 1 week, 1 day ago
On Sat, Nov 22, 2025 at 07:26:52PM +0800, Tiezhu Yang wrote:
> On 11/22/25 09:35, Nathan Chancellor wrote:
> > On Wed, Nov 19, 2025 at 12:27:08PM +0800, Tiezhu Yang wrote:
> > ...
> > > index 433849ff7529..e72d3254b93f 100755
> > > --- a/scripts/link-vmlinux.sh
> > > +++ b/scripts/link-vmlinux.sh
> > > @@ -61,12 +61,11 @@ vmlinux_link()
> > >   	shift
> > >   	if is_enabled CONFIG_LTO_CLANG || is_enabled CONFIG_X86_KERNEL_IBT; then
> > > -		# Use vmlinux.o instead of performing the slow LTO link again.
> > 
> > Why is this comment getting removed?
> 
> When using KBUILD_VMLINUX_LIBS_PRELINK or KBUILD_VMLINUX_LIBS_FINAL,
> drivers/firmware/efi/libstub/lib.a needs to be linked to the final
> vmlinux with vmlinux.o, so I thought the above comment is not proper,
> it I misunderstood it, I will keep the comment as is.

I think this comment is referring to the use of vmlinux.o instead of
vmlinux.a, nothing to do with libs=, so I think the comment should
remain.

> > >   		objs=vmlinux.o
> > > -		libs=
> > > +		libs="${KBUILD_VMLINUX_LIBS_PRELINK}"
> > >   	else
> > >   		objs=vmlinux.a
> > > -		libs="${KBUILD_VMLINUX_LIBS}"
> > > +		libs="${KBUILD_VMLINUX_LIBS} ${KBUILD_VMLINUX_LIBS_PRELINK}"
> > >   	fi
> > >   	if is_enabled CONFIG_GENERIC_BUILTIN_DTB; then
> 
> FYI, the previous patch is still under discussion yesterday [1],
> I do not know whether this patch is necessary and what is the next
> step [2].
> 
> [1] https://lore.kernel.org/loongarch/CAAhV-H4AasfFet_Gi_mVyte3RPMH3qBS73dBfF-=Gd7HJ6ZPEw@mail.gmail.com/
> [2] https://lore.kernel.org/loongarch/39617a3e-c476-abac-8425-bbcece769cdb@loongson.cn/

Thanks, we will wait to see the conclusion of those threads before
applying this.

Cheers,
Nathan
Re: [PATCH v1] kbuild: Add KBUILD_VMLINUX_LIBS_PRELINK
Posted by Nathan Chancellor 1 week, 3 days ago
On Wed, Nov 19, 2025 at 12:27:08PM +0800, Tiezhu Yang wrote:
> In order to only link libstub to the final vmlinux, it can not use the
> current KBUILD_VMLINUX_LIBS, just add KBUILD_VMLINUX_LIBS_PRELINK. This
> is preparation for later patch, no functionality change.
> 
> Link: https://lore.kernel.org/lkml/pq4h7jgndnt6p45lj4kgubxjd5gidfetugcuf5rcxzxxanzetd@6rrlpjnjsmuy/
> Suggested-by: Josh Poimboeuf <jpoimboe@kernel.org>
> Signed-off-by: Tiezhu Yang <yangtiezhu@loongson.cn>
> ---
> There is a long discussion in the previous patch:
> https://lore.kernel.org/lkml/20250928085506.4471-1-yangtiezhu@loongson.cn/
> 
> This version is based on 6.18-rc6, split the generic parts out
> into a separate patch to avoid merge conflicts, the other parts
> will send out after the merge window.
> 
>  Makefile                | 1 +
>  scripts/link-vmlinux.sh | 5 ++---
>  2 files changed, 3 insertions(+), 3 deletions(-)
> 
> diff --git a/Makefile b/Makefile
> index d763c2c75cdb..69485f47b794 100644
> --- a/Makefile
> +++ b/Makefile
> @@ -1199,6 +1199,7 @@ KBUILD_VMLINUX_OBJS := built-in.a $(patsubst %/, %/lib.a, $(filter %/, $(libs-y)
>  KBUILD_VMLINUX_LIBS := $(filter-out %/, $(libs-y))
>  
>  export KBUILD_VMLINUX_LIBS
> +export KBUILD_VMLINUX_LIBS_PRELINK

This variable name is a little confusing to me since they do get added
to vmlinux during linking, not before. I am not sure of a better one
though, maybe KBUILD_VMLINUX_LIBS_FINAL? It may also make sense to
introduce similar syntax to the existing libs-y syntax, maybe

  final-libs-y

or something like that?

Also, since these objects are no longer in KBUILD_VMLINUX_LIBS, does
this new variable need to be added to any of the other places in the
build system that use KBUILD_VMLINUX_LIBS for things such as build
dependencies?

>  export KBUILD_LDS          := arch/$(SRCARCH)/kernel/vmlinux.lds
>  
>  ifdef CONFIG_TRIM_UNUSED_KSYMS
> diff --git a/scripts/link-vmlinux.sh b/scripts/link-vmlinux.sh
> index 433849ff7529..e72d3254b93f 100755
> --- a/scripts/link-vmlinux.sh
> +++ b/scripts/link-vmlinux.sh
> @@ -61,12 +61,11 @@ vmlinux_link()
>  	shift
>  
>  	if is_enabled CONFIG_LTO_CLANG || is_enabled CONFIG_X86_KERNEL_IBT; then
> -		# Use vmlinux.o instead of performing the slow LTO link again.
>  		objs=vmlinux.o
> -		libs=
> +		libs="${KBUILD_VMLINUX_LIBS_PRELINK}"
>  	else
>  		objs=vmlinux.a
> -		libs="${KBUILD_VMLINUX_LIBS}"
> +		libs="${KBUILD_VMLINUX_LIBS} ${KBUILD_VMLINUX_LIBS_PRELINK}"
>  	fi
>  
>  	if is_enabled CONFIG_GENERIC_BUILTIN_DTB; then
> -- 
> 2.42.0
>
Re: [PATCH v1] kbuild: Add KBUILD_VMLINUX_LIBS_PRELINK
Posted by Josh Poimboeuf 1 week, 3 days ago
On Fri, Nov 21, 2025 at 12:01:40AM -0700, Nathan Chancellor wrote:
> > @@ -1199,6 +1199,7 @@ KBUILD_VMLINUX_OBJS := built-in.a $(patsubst %/, %/lib.a, $(filter %/, $(libs-y)
> >  KBUILD_VMLINUX_LIBS := $(filter-out %/, $(libs-y))
> >  
> >  export KBUILD_VMLINUX_LIBS
> > +export KBUILD_VMLINUX_LIBS_PRELINK
> 
> This variable name is a little confusing to me since they do get added
> to vmlinux during linking, not before. I am not sure of a better one
> though, maybe KBUILD_VMLINUX_LIBS_FINAL?

I think that name was my idea.  Not sure what I was thinking, I guess
"prelink" was somehow meant to imply after the vmlinux.o whole-archive
link but before the "final" link.

"final" is indeed better.

> It may also make sense to
> introduce similar syntax to the existing libs-y syntax, maybe
> 
>   final-libs-y
> 
> or something like that?

Yeah, I suppose that would mirror how KBUILD_VMLINUX_LIBS is wired up.

> Also, since these objects are no longer in KBUILD_VMLINUX_LIBS, does
> this new variable need to be added to any of the other places in the
> build system that use KBUILD_VMLINUX_LIBS for things such as build
> dependencies?

Not sure about that one.  drivers/firmware/efi/libstub/lib.a is getting
built regardless so there must already be an implicit dependency?

-- 
Josh
Re: [PATCH v1] kbuild: Add KBUILD_VMLINUX_LIBS_PRELINK
Posted by Nathan Chancellor 1 week, 3 days ago
On Fri, Nov 21, 2025 at 10:42:47AM -0800, Josh Poimboeuf wrote:
> On Fri, Nov 21, 2025 at 12:01:40AM -0700, Nathan Chancellor wrote:
> > > @@ -1199,6 +1199,7 @@ KBUILD_VMLINUX_OBJS := built-in.a $(patsubst %/, %/lib.a, $(filter %/, $(libs-y)
> > >  KBUILD_VMLINUX_LIBS := $(filter-out %/, $(libs-y))
> > >  
> > >  export KBUILD_VMLINUX_LIBS
> > > +export KBUILD_VMLINUX_LIBS_PRELINK
> > 
> > This variable name is a little confusing to me since they do get added
> > to vmlinux during linking, not before. I am not sure of a better one
> > though, maybe KBUILD_VMLINUX_LIBS_FINAL?
> 
> I think that name was my idea.  Not sure what I was thinking, I guess
> "prelink" was somehow meant to imply after the vmlinux.o whole-archive
> link but before the "final" link.

Yeah I could see where you were going with that but glad we agree final
is better heh.

> "final" is indeed better.
> 
> > It may also make sense to
> > introduce similar syntax to the existing libs-y syntax, maybe
> > 
> >   final-libs-y
> > 
> > or something like that?
> 
> Yeah, I suppose that would mirror how KBUILD_VMLINUX_LIBS is wired up.

On second thought, it might be less complicated to keep it this way...

> > Also, since these objects are no longer in KBUILD_VMLINUX_LIBS, does
> > this new variable need to be added to any of the other places in the
> > build system that use KBUILD_VMLINUX_LIBS for things such as build
> > dependencies?
> 
> Not sure about that one.  drivers/firmware/efi/libstub/lib.a is getting
> built regardless so there must already be an implicit dependency?

I think the explicit dependency currently comes from

  vmlinux_o: vmlinux.a $(KBUILD_VMLINUX_LIBS)

in Makefile. I think something like

diff --git a/scripts/Makefile.vmlinux b/scripts/Makefile.vmlinux
index cd788cac9d91..38b5f0ba9a4b 100644
--- a/scripts/Makefile.vmlinux
+++ b/scripts/Makefile.vmlinux
@@ -68,7 +68,7 @@ cmd_link_vmlinux =							\
 	$(if $(ARCH_POSTLINK), $(MAKE) -f $(ARCH_POSTLINK) $@, true)
 
 targets += vmlinux.unstripped .vmlinux.export.o
-vmlinux.unstripped: scripts/link-vmlinux.sh vmlinux.o .vmlinux.export.o $(KBUILD_LDS) FORCE
+vmlinux.unstripped: scripts/link-vmlinux.sh vmlinux.o .vmlinux.export.o $(KBUILD_LDS) $(KBUILD_VMLINUX_LIBS_FINAL) FORCE
 	+$(call if_changed_dep,link_vmlinux)
 ifdef CONFIG_DEBUG_INFO_BTF
 vmlinux.unstripped: $(RESOLVE_BTFIDS)

is needed to ensure that an updated drivers/firmware/efi/libstub/lib.a
results in vmlinux getting regenerated (at least according to my
testing).

Cheers,
Nathan