2018-03-29 00:50:36

by Laura Abbott

[permalink] [raw]
Subject: [PATCH 0/3] HOSTCFLAGS and HOSTLDFLAGS from the environment

Hi,

Someone noted that scripts/tools userspace binaries packaged as part of
the Fedora kernel were not picking up the recommended compiler flags
(https://src.fedoraproject.org/rpms/redhat-rpm-config/blob/master/f/buildflags.md)
This series lets the host ld and C flags be set on the command line
similar to the kernel flags:

make AFLAGS_HOSTCFLAGS="%{build_cflags}" AFLAGS_HOSTLDFLAGS="%{build_ldflags}"

There's one more annoying case I didn't quite figure out with
tools/objtool/fixdep since that's linked with ld directly but this
covers the initial set of reports.

Thanks,
Laura

Laura Abbott (3):
kbuild: Support HOSTLDFLAGS
objtool: Support HOSTCFLAGS and HOSTLDFLAGS
kbuild: Allow passing additional HOSTCFLAGS and HOSTLDFLAGS

Documentation/kbuild/kbuild.txt | 9 +++++++++
Makefile | 3 +++
scripts/Makefile.host | 6 +++---
tools/build/Makefile.build | 2 +-
tools/objtool/Makefile | 5 +++--
5 files changed, 19 insertions(+), 6 deletions(-)

--
2.16.2



2018-03-29 00:49:33

by Laura Abbott

[permalink] [raw]
Subject: [PATCH 3/3] kbuild: Allow passing additional HOSTCFLAGS and HOSTLDFLAGS


Similar to AFLAGS_KBUILD, there may be uses (e.g. hardening) for passing in
additional flags to host programs. Allow these to be passed in from the
environment.

Signed-off-by: Laura Abbott <[email protected]>
---
Documentation/kbuild/kbuild.txt | 9 +++++++++
Makefile | 3 +++
2 files changed, 12 insertions(+)

diff --git a/Documentation/kbuild/kbuild.txt b/Documentation/kbuild/kbuild.txt
index ac2363ea05c5..3751a4bc8596 100644
--- a/Documentation/kbuild/kbuild.txt
+++ b/Documentation/kbuild/kbuild.txt
@@ -24,6 +24,15 @@ KAFLAGS
--------------------------------------------------
Additional options to the assembler (for built-in and modules).

+AFLAGS_HOSTCFLAGS
+--------------------------------------------------
+Additional options passed to the compiler when building host programs.
+
+AFLAGS_HOSTLDFLAGS
+--------------------------------------------------
+Additional options passed to the linker (through the compiler) when buidling
+host programs.
+
AFLAGS_MODULE
--------------------------------------------------
Additional module specific options to use for $(AS).
diff --git a/Makefile b/Makefile
index 7ba478ab8c82..2cab3f8d489c 100644
--- a/Makefile
+++ b/Makefile
@@ -367,6 +367,9 @@ HOSTCXXFLAGS := -O2 $(HOST_LFS_CFLAGS)
HOSTLDFLAGS := $(HOST_LFS_LDFLAGS)
HOST_LOADLIBES := $(HOST_LFS_LIBS)

+HOSTCFLAGS += $(AFLAGS_HOSTCFLAGS)
+HOSTLDFLAGS += $(AFLAGS_HOSTLDFLAGS)
+
# Make variables (CC, etc...)
AS = $(CROSS_COMPILE)as
LD = $(CROSS_COMPILE)ld
--
2.16.2


2018-03-29 00:49:44

by Laura Abbott

[permalink] [raw]
Subject: [PATCH 1/3] kbuild: Support HOSTLDFLAGS


In addition to HOSTCFLAGS, there's HOSTLDFLAGS. Ensure these get passed to
calls to build host binaries.

Signed-off-by: Laura Abbott <[email protected]>
---
scripts/Makefile.host | 6 +++---
tools/build/Makefile.build | 2 +-
2 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/scripts/Makefile.host b/scripts/Makefile.host
index e6dc6ae2d7c4..a3a0e2282a56 100644
--- a/scripts/Makefile.host
+++ b/scripts/Makefile.host
@@ -84,7 +84,7 @@ hostcxx_flags = -Wp,-MD,$(depfile) $(__hostcxx_flags)
# Create executable from a single .c file
# host-csingle -> Executable
quiet_cmd_host-csingle = HOSTCC $@
- cmd_host-csingle = $(HOSTCC) $(hostc_flags) -o $@ $< \
+ cmd_host-csingle = $(HOSTCC) $(HOSTLDFLAGS) $(hostc_flags) -o $@ $< \
$(HOST_LOADLIBES) $(HOSTLOADLIBES_$(@F))
$(host-csingle): $(obj)/%: $(src)/%.c FORCE
$(call if_changed_dep,host-csingle)
@@ -102,7 +102,7 @@ $(call multi_depend, $(host-cmulti), , -objs)
# Create .o file from a single .c file
# host-cobjs -> .o
quiet_cmd_host-cobjs = HOSTCC $@
- cmd_host-cobjs = $(HOSTCC) $(hostc_flags) -c -o $@ $<
+ cmd_host-cobjs = $(HOSTCC) $(HOSTLDFLAGS) $(hostc_flags) -c -o $@ $<
$(host-cobjs): $(obj)/%.o: $(src)/%.c FORCE
$(call if_changed_dep,host-cobjs)

@@ -126,7 +126,7 @@ $(host-cxxobjs): $(obj)/%.o: $(src)/%.cc FORCE
# Compile .c file, create position independent .o file
# host-cshobjs -> .o
quiet_cmd_host-cshobjs = HOSTCC -fPIC $@
- cmd_host-cshobjs = $(HOSTCC) $(hostc_flags) -fPIC -c -o $@ $<
+ cmd_host-cshobjs = $(HOSTCC) $(HOSTLDFLAGS) $(hostc_flags) -fPIC -c -o $@ $<
$(host-cshobjs): $(obj)/%.o: $(src)/%.c FORCE
$(call if_changed_dep,host-cshobjs)

diff --git a/tools/build/Makefile.build b/tools/build/Makefile.build
index cd72016c3cfa..cab55f0d90e1 100644
--- a/tools/build/Makefile.build
+++ b/tools/build/Makefile.build
@@ -64,7 +64,7 @@ quiet_cmd_cc_o_c = CC $@
cmd_cc_o_c = $(CC) $(c_flags) -c -o $@ $<

quiet_cmd_host_cc_o_c = HOSTCC $@
- cmd_host_cc_o_c = $(HOSTCC) $(host_c_flags) -c -o $@ $<
+ cmd_host_cc_o_c = $(HOSTCC) $(HOSTLDFLAGS) $(host_c_flags) -c -o $@ $<

quiet_cmd_cxx_o_c = CXX $@
cmd_cxx_o_c = $(CXX) $(cxx_flags) -c -o $@ $<
--
2.16.2


2018-03-29 00:50:04

by Laura Abbott

[permalink] [raw]
Subject: [PATCH 2/3] objtool: Support HOSTCFLAGS and HOSTLDFLAGS

It may be useful to compile host programs with different flags (e.g.
hardening). Ensure that objtool picks up the appropriate flags.

Signed-off-by: Laura Abbott <[email protected]>
---
tools/objtool/Makefile | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/tools/objtool/Makefile b/tools/objtool/Makefile
index e6acc281dd37..0ff3bcac1ca9 100644
--- a/tools/objtool/Makefile
+++ b/tools/objtool/Makefile
@@ -31,8 +31,9 @@ INCLUDES := -I$(srctree)/tools/include \
-I$(srctree)/tools/arch/$(HOSTARCH)/include/uapi \
-I$(srctree)/tools/objtool/arch/$(ARCH)/include
WARNINGS := $(EXTRA_WARNINGS) -Wno-switch-default -Wno-switch-enum -Wno-packed
-CFLAGS += -Wall -Werror $(WARNINGS) -fomit-frame-pointer -O2 -g $(INCLUDES)
-LDFLAGS += -lelf $(LIBSUBCMD)
+CFLAGS += -Wall -Werror $(WARNINGS) $(HOSTCFLAGS) -fomit-frame-pointer -O2 -g \
+ $(INCLUDES)
+LDFLAGS += -lelf $(LIBSUBCMD) $(HOSTLDFLAGS)

# Allow old libelf to be used:
elfshdr := $(shell echo '\#include <libelf.h>' | $(CC) $(CFLAGS) -x c -E - | grep elf_getshdr)
--
2.16.2


2018-04-05 04:45:45

by Masahiro Yamada

[permalink] [raw]
Subject: Re: [PATCH 2/3] objtool: Support HOSTCFLAGS and HOSTLDFLAGS

2018-03-29 9:48 GMT+09:00 Laura Abbott <[email protected]>:
> It may be useful to compile host programs with different flags (e.g.
> hardening). Ensure that objtool picks up the appropriate flags.
>
> Signed-off-by: Laura Abbott <[email protected]>
> ---


I saw some similar patches before.

I thought they are fixing this way
https://patchwork.kernel.org/patch/10251259/

but, I am not tacking the progress.




> tools/objtool/Makefile | 5 +++--
> 1 file changed, 3 insertions(+), 2 deletions(-)
>
> diff --git a/tools/objtool/Makefile b/tools/objtool/Makefile
> index e6acc281dd37..0ff3bcac1ca9 100644
> --- a/tools/objtool/Makefile
> +++ b/tools/objtool/Makefile
> @@ -31,8 +31,9 @@ INCLUDES := -I$(srctree)/tools/include \
> -I$(srctree)/tools/arch/$(HOSTARCH)/include/uapi \
> -I$(srctree)/tools/objtool/arch/$(ARCH)/include
> WARNINGS := $(EXTRA_WARNINGS) -Wno-switch-default -Wno-switch-enum -Wno-packed
> -CFLAGS += -Wall -Werror $(WARNINGS) -fomit-frame-pointer -O2 -g $(INCLUDES)
> -LDFLAGS += -lelf $(LIBSUBCMD)
> +CFLAGS += -Wall -Werror $(WARNINGS) $(HOSTCFLAGS) -fomit-frame-pointer -O2 -g \
> + $(INCLUDES)
> +LDFLAGS += -lelf $(LIBSUBCMD) $(HOSTLDFLAGS)
>
> # Allow old libelf to be used:
> elfshdr := $(shell echo '\#include <libelf.h>' | $(CC) $(CFLAGS) -x c -E - | grep elf_getshdr)
> --
> 2.16.2
>
> --
> To unsubscribe from this list: send the line "unsubscribe linux-kbuild" in
> the body of a message to [email protected]
> More majordomo info at http://vger.kernel.org/majordomo-info.html



--
Best Regards
Masahiro Yamada

2018-04-05 05:03:00

by Masahiro Yamada

[permalink] [raw]
Subject: Re: [PATCH 3/3] kbuild: Allow passing additional HOSTCFLAGS and HOSTLDFLAGS

2018-03-29 9:48 GMT+09:00 Laura Abbott <[email protected]>:
>
> Similar to AFLAGS_KBUILD, there may be uses (e.g. hardening) for passing in
> additional flags to host programs. Allow these to be passed in from the
> environment.
>
> Signed-off-by: Laura Abbott <[email protected]>
> ---
> Documentation/kbuild/kbuild.txt | 9 +++++++++
> Makefile | 3 +++
> 2 files changed, 12 insertions(+)
>
> diff --git a/Documentation/kbuild/kbuild.txt b/Documentation/kbuild/kbuild.txt
> index ac2363ea05c5..3751a4bc8596 100644
> --- a/Documentation/kbuild/kbuild.txt
> +++ b/Documentation/kbuild/kbuild.txt
> @@ -24,6 +24,15 @@ KAFLAGS
> --------------------------------------------------
> Additional options to the assembler (for built-in and modules).
>
> +AFLAGS_HOSTCFLAGS
> +--------------------------------------------------
> +Additional options passed to the compiler when building host programs.
> +
> +AFLAGS_HOSTLDFLAGS
> +--------------------------------------------------
> +Additional options passed to the linker (through the compiler) when buidling
> +host programs.
> +


I am afraid you misunderstood the meaning of 'AFLAGS'.

AFLAGS is not 'Additional flags', but 'Assembler flags'
that are used for compiling *.S files.

AFLAGS for host programs is weird.



I see similar proposals from different people.

I replied like follows:
https://lkml.org/lkml/2018/2/28/178

However, Robin seems busy lately.

I will wait a bit, then
if nobody does this, I may do it.




> AFLAGS_MODULE
> --------------------------------------------------
> Additional module specific options to use for $(AS).
> diff --git a/Makefile b/Makefile
> index 7ba478ab8c82..2cab3f8d489c 100644
> --- a/Makefile
> +++ b/Makefile
> @@ -367,6 +367,9 @@ HOSTCXXFLAGS := -O2 $(HOST_LFS_CFLAGS)
> HOSTLDFLAGS := $(HOST_LFS_LDFLAGS)
> HOST_LOADLIBES := $(HOST_LFS_LIBS)
>
> +HOSTCFLAGS += $(AFLAGS_HOSTCFLAGS)
> +HOSTLDFLAGS += $(AFLAGS_HOSTLDFLAGS)
> +
> # Make variables (CC, etc...)
> AS = $(CROSS_COMPILE)as
> LD = $(CROSS_COMPILE)ld
> --
> 2.16.2
>
> --
> To unsubscribe from this list: send the line "unsubscribe linux-kbuild" in
> the body of a message to [email protected]
> More majordomo info at http://vger.kernel.org/majordomo-info.html



--
Best Regards
Masahiro Yamada

2018-04-05 05:16:22

by Masahiro Yamada

[permalink] [raw]
Subject: Re: [PATCH 1/3] kbuild: Support HOSTLDFLAGS

2018-03-29 9:48 GMT+09:00 Laura Abbott <[email protected]>:
>
> In addition to HOSTCFLAGS, there's HOSTLDFLAGS. Ensure these get passed to
> calls to build host binaries.
>
> Signed-off-by: Laura Abbott <[email protected]>
> ---
> scripts/Makefile.host | 6 +++---
> tools/build/Makefile.build | 2 +-
> 2 files changed, 4 insertions(+), 4 deletions(-)
>
> diff --git a/scripts/Makefile.host b/scripts/Makefile.host
> index e6dc6ae2d7c4..a3a0e2282a56 100644
> --- a/scripts/Makefile.host
> +++ b/scripts/Makefile.host
> @@ -84,7 +84,7 @@ hostcxx_flags = -Wp,-MD,$(depfile) $(__hostcxx_flags)
> # Create executable from a single .c file
> # host-csingle -> Executable
> quiet_cmd_host-csingle = HOSTCC $@
> - cmd_host-csingle = $(HOSTCC) $(hostc_flags) -o $@ $< \
> + cmd_host-csingle = $(HOSTCC) $(HOSTLDFLAGS) $(hostc_flags) -o $@ $< \
> $(HOST_LOADLIBES) $(HOSTLOADLIBES_$(@F))
> $(host-csingle): $(obj)/%: $(src)/%.c FORCE
> $(call if_changed_dep,host-csingle)


This hunk is correct, but Robin posted this.
https://patchwork.kernel.org/patch/10243073/

Somehow it fell into a crack, and I missed to pick it up.
I will apply it lately.




> @@ -102,7 +102,7 @@ $(call multi_depend, $(host-cmulti), , -objs)
> # Create .o file from a single .c file
> # host-cobjs -> .o
> quiet_cmd_host-cobjs = HOSTCC $@
> - cmd_host-cobjs = $(HOSTCC) $(hostc_flags) -c -o $@ $<
> + cmd_host-cobjs = $(HOSTCC) $(HOSTLDFLAGS) $(hostc_flags) -c -o $@ $<
> $(host-cobjs): $(obj)/%.o: $(src)/%.c FORCE
> $(call if_changed_dep,host-cobjs)
>

This does not involve the link stage.
You should not do this.


> @@ -126,7 +126,7 @@ $(host-cxxobjs): $(obj)/%.o: $(src)/%.cc FORCE
> # Compile .c file, create position independent .o file
> # host-cshobjs -> .o
> quiet_cmd_host-cshobjs = HOSTCC -fPIC $@
> - cmd_host-cshobjs = $(HOSTCC) $(hostc_flags) -fPIC -c -o $@ $<
> + cmd_host-cshobjs = $(HOSTCC) $(HOSTLDFLAGS) $(hostc_flags) -fPIC -c -o $@ $<
> $(host-cshobjs): $(obj)/%.o: $(src)/%.c FORCE
> $(call if_changed_dep,host-cshobjs)
>
> diff --git a/tools/build/Makefile.build b/tools/build/Makefile.build
> index cd72016c3cfa..cab55f0d90e1 100644
> --- a/tools/build/Makefile.build
> +++ b/tools/build/Makefile.build
> @@ -64,7 +64,7 @@ quiet_cmd_cc_o_c = CC $@
> cmd_cc_o_c = $(CC) $(c_flags) -c -o $@ $<
>
> quiet_cmd_host_cc_o_c = HOSTCC $@
> - cmd_host_cc_o_c = $(HOSTCC) $(host_c_flags) -c -o $@ $<
> + cmd_host_cc_o_c = $(HOSTCC) $(HOSTLDFLAGS) $(host_c_flags) -c -o $@ $<
>
> quiet_cmd_cxx_o_c = CXX $@
> cmd_cxx_o_c = $(CXX) $(cxx_flags) -c -o $@ $<



--
Best Regards
Masahiro Yamada

2018-04-05 21:55:27

by Laura Abbott

[permalink] [raw]
Subject: Re: [PATCH 3/3] kbuild: Allow passing additional HOSTCFLAGS and HOSTLDFLAGS

On 04/04/2018 09:59 PM, Masahiro Yamada wrote:
> 2018-03-29 9:48 GMT+09:00 Laura Abbott <[email protected]>:
>>
>> Similar to AFLAGS_KBUILD, there may be uses (e.g. hardening) for passing in
>> additional flags to host programs. Allow these to be passed in from the
>> environment.
>>
>> Signed-off-by: Laura Abbott <[email protected]>
>> ---
>> Documentation/kbuild/kbuild.txt | 9 +++++++++
>> Makefile | 3 +++
>> 2 files changed, 12 insertions(+)
>>
>> diff --git a/Documentation/kbuild/kbuild.txt b/Documentation/kbuild/kbuild.txt
>> index ac2363ea05c5..3751a4bc8596 100644
>> --- a/Documentation/kbuild/kbuild.txt
>> +++ b/Documentation/kbuild/kbuild.txt
>> @@ -24,6 +24,15 @@ KAFLAGS
>> --------------------------------------------------
>> Additional options to the assembler (for built-in and modules).
>>
>> +AFLAGS_HOSTCFLAGS
>> +--------------------------------------------------
>> +Additional options passed to the compiler when building host programs.
>> +
>> +AFLAGS_HOSTLDFLAGS
>> +--------------------------------------------------
>> +Additional options passed to the linker (through the compiler) when buidling
>> +host programs.
>> +
>
>
> I am afraid you misunderstood the meaning of 'AFLAGS'.
>
> AFLAGS is not 'Additional flags', but 'Assembler flags'
> that are used for compiling *.S files.
>
> AFLAGS for host programs is weird.
>
>
>
> I see similar proposals from different people.
>
> I replied like follows:
> https://lkml.org/lkml/2018/2/28/178
>
> However, Robin seems busy lately.
>
> I will wait a bit, then
> if nobody does this, I may do it.
>

Thanks for the pointer. I'll keep an eye out for those patches.

>
>
>
>> AFLAGS_MODULE
>> --------------------------------------------------
>> Additional module specific options to use for $(AS).
>> diff --git a/Makefile b/Makefile
>> index 7ba478ab8c82..2cab3f8d489c 100644
>> --- a/Makefile
>> +++ b/Makefile
>> @@ -367,6 +367,9 @@ HOSTCXXFLAGS := -O2 $(HOST_LFS_CFLAGS)
>> HOSTLDFLAGS := $(HOST_LFS_LDFLAGS)
>> HOST_LOADLIBES := $(HOST_LFS_LIBS)
>>
>> +HOSTCFLAGS += $(AFLAGS_HOSTCFLAGS)
>> +HOSTLDFLAGS += $(AFLAGS_HOSTLDFLAGS)
>> +
>> # Make variables (CC, etc...)
>> AS = $(CROSS_COMPILE)as
>> LD = $(CROSS_COMPILE)ld
>> --
>> 2.16.2
>>
>> --
>> To unsubscribe from this list: send the line "unsubscribe linux-kbuild" in
>> the body of a message to [email protected]
>> More majordomo info at http://vger.kernel.org/majordomo-info.html
>
>
>


2018-04-05 22:07:26

by Laura Abbott

[permalink] [raw]
Subject: Re: [PATCH 2/3] objtool: Support HOSTCFLAGS and HOSTLDFLAGS

On 04/04/2018 09:42 PM, Masahiro Yamada wrote:
> 2018-03-29 9:48 GMT+09:00 Laura Abbott <[email protected]>:
>> It may be useful to compile host programs with different flags (e.g.
>> hardening). Ensure that objtool picks up the appropriate flags.
>>
>> Signed-off-by: Laura Abbott <[email protected]>
>> ---
>
>
> I saw some similar patches before.
>
> I thought they are fixing this way
> https://patchwork.kernel.org/patch/10251259/
>
> but, I am not tacking the progress.
>
>
>

That looks like a different issue since it's still not going to
pick up the HOST{C,LD}FLAGS.

>
>> tools/objtool/Makefile | 5 +++--
>> 1 file changed, 3 insertions(+), 2 deletions(-)
>>
>> diff --git a/tools/objtool/Makefile b/tools/objtool/Makefile
>> index e6acc281dd37..0ff3bcac1ca9 100644
>> --- a/tools/objtool/Makefile
>> +++ b/tools/objtool/Makefile
>> @@ -31,8 +31,9 @@ INCLUDES := -I$(srctree)/tools/include \
>> -I$(srctree)/tools/arch/$(HOSTARCH)/include/uapi \
>> -I$(srctree)/tools/objtool/arch/$(ARCH)/include
>> WARNINGS := $(EXTRA_WARNINGS) -Wno-switch-default -Wno-switch-enum -Wno-packed
>> -CFLAGS += -Wall -Werror $(WARNINGS) -fomit-frame-pointer -O2 -g $(INCLUDES)
>> -LDFLAGS += -lelf $(LIBSUBCMD)
>> +CFLAGS += -Wall -Werror $(WARNINGS) $(HOSTCFLAGS) -fomit-frame-pointer -O2 -g \
>> + $(INCLUDES)
>> +LDFLAGS += -lelf $(LIBSUBCMD) $(HOSTLDFLAGS)
>>
>> # Allow old libelf to be used:
>> elfshdr := $(shell echo '\#include <libelf.h>' | $(CC) $(CFLAGS) -x c -E - | grep elf_getshdr)
>> --
>> 2.16.2
>>
>> --
>> To unsubscribe from this list: send the line "unsubscribe linux-kbuild" in
>> the body of a message to [email protected]
>> More majordomo info at http://vger.kernel.org/majordomo-info.html
>
>
>


2018-04-06 17:30:54

by Josh Poimboeuf

[permalink] [raw]
Subject: Re: [PATCH 2/3] objtool: Support HOSTCFLAGS and HOSTLDFLAGS

On Wed, Mar 28, 2018 at 05:48:04PM -0700, Laura Abbott wrote:
> It may be useful to compile host programs with different flags (e.g.
> hardening). Ensure that objtool picks up the appropriate flags.
>
> Signed-off-by: Laura Abbott <[email protected]>
> ---
> tools/objtool/Makefile | 5 +++--
> 1 file changed, 3 insertions(+), 2 deletions(-)
>
> diff --git a/tools/objtool/Makefile b/tools/objtool/Makefile
> index e6acc281dd37..0ff3bcac1ca9 100644
> --- a/tools/objtool/Makefile
> +++ b/tools/objtool/Makefile
> @@ -31,8 +31,9 @@ INCLUDES := -I$(srctree)/tools/include \
> -I$(srctree)/tools/arch/$(HOSTARCH)/include/uapi \
> -I$(srctree)/tools/objtool/arch/$(ARCH)/include
> WARNINGS := $(EXTRA_WARNINGS) -Wno-switch-default -Wno-switch-enum -Wno-packed
> -CFLAGS += -Wall -Werror $(WARNINGS) -fomit-frame-pointer -O2 -g $(INCLUDES)
> -LDFLAGS += -lelf $(LIBSUBCMD)
> +CFLAGS += -Wall -Werror $(WARNINGS) $(HOSTCFLAGS) -fomit-frame-pointer -O2 -g \
> + $(INCLUDES)

HOSTCFLAGS already has '-Wall', '-fomit-frame-pointer', and '-O2', so we
can remove the setting of those in CFLAGS here.

--
Josh