Skip to contentSkip to conversation

HOSTCFLAGS and HOSTLDFLAGS from the environment

PATCH In review Laura Abbott 3 participants 10 messages Mar 29, 2018 – Apr 6, 2018

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

cover 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(-)
Signature · 1 line
2.16.2
#1 lore raw

PATCH 1/3 kbuild: Support HOSTLDFLAGS

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

---
 scripts/Makefile.host      | 6 +++---
 tools/build/Makefile.build | 2 +-
 2 files changed, 4 insertions(+), 4 deletions(-)
scripts/Makefile.host +3 -3
index e6dc6ae2d7c4..a3a0e2282a56 100644
@@ -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)
 
tools/build/Makefile.build +1 -1
index cd72016c3cfa..cab55f0d90e1 100644
@@ -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 $@ $<
Signature · 1 line
2.16.2
#2 lore raw

Re: [PATCH 1/3] kbuild: Support HOSTLDFLAGS

2018-03-29 9:48 GMT+09:00 Laura Abbott <[email protected]>:

Quoted 15 lines

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

---
 scripts/Makefile.host      | 6 +++---
 tools/build/Makefile.build | 2 +-
 2 files changed, 4 insertions(+), 4 deletions(-)
scripts/Makefile.host +0 -0
index e6dc6ae2d7c4..a3a0e2282a56 100644

scripts/Makefile.host hostcxx_flags = -Wp,-MD,$(depfile) $(__hostcxx_flags) · line 90

Quoted 8 lines
scripts/Makefile.host +1 -1
@@ -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.

scripts/Makefile.host $(call multi_depend, $(host-cmulti), , -objs) · line 108

Quoted 9 lines
scripts/Makefile.host +1 -1
@@ -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.

Quoted 22 lines
scripts/Makefile.host +1 -1
@@ -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)
 
tools/build/Makefile.build +1 -1
index cd72016c3cfa..cab55f0d90e1 100644
@@ -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 $@ $<
Signature · 2 lines
Best Regards
Masahiro Yamada
#3 lore raw

PATCH 2/3 objtool: Support HOSTCFLAGS and HOSTLDFLAGS

In reply to Laura Abbott

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

---
 tools/objtool/Makefile | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
tools/objtool/Makefile +3 -2
index e6acc281dd37..0ff3bcac1ca9 100644
@@ -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)
Signature · 1 line
2.16.2
#4 lore raw

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

2018-03-29 9:48 GMT+09:00 Laura Abbott <[email protected]>:

Quoted 5 lines

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

---

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.

Quoted 26 lines
 tools/objtool/Makefile | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
tools/objtool/Makefile +3 -3
index e6acc281dd37..0ff3bcac1ca9 100644
@@ -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

Signature · 2 lines
Best Regards
Masahiro Yamada
#5 lore raw

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

Quoted 8 lines · Masahiro Yamada

2018-03-29 9:48 GMT+09:00 Laura Abbott <[email protected]>:

Quoted 5 lines

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

---

Quoted 9 lines

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.

Quoted 30 lines
Quoted 27 lines
  tools/objtool/Makefile | 5 +++--
  1 file changed, 3 insertions(+), 2 deletions(-)
tools/objtool/Makefile +3 -3
index e6acc281dd37..0ff3bcac1ca9 100644
@@ -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

#6 lore raw

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

In reply to Laura Abbott
Quoted 13 lines · Laura Abbott

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

---
 tools/objtool/Makefile | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
tools/objtool/Makefile +0 -0
index e6acc281dd37..0ff3bcac1ca9 100644

tools/objtool/Makefile INCLUDES := -I$(srctree)/tools/include \ · line 35

Quoted 7 lines
tools/objtool/Makefile +2 -2
@@ -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.

Signature · 1 line
Josh
#7 lore raw

PATCH 3/3 kbuild: Allow passing additional HOSTCFLAGS and HOSTLDFLAGS

In reply to Laura Abbott

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.

---
 Documentation/kbuild/kbuild.txt | 9 +++++++++
 Makefile                        | 3 +++
 2 files changed, 12 insertions(+)
Documentation/kbuild/kbuild.txt +9 -0
index ac2363ea05c5..3751a4bc8596 100644
@@ -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).
Makefile +3 -0
index 7ba478ab8c82..2cab3f8d489c 100644
@@ -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
Signature · 1 line
2.16.2
#8 lore raw

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

2018-03-29 9:48 GMT+09:00 Laura Abbott <[email protected]>:

Quoted 18 lines

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.

---
 Documentation/kbuild/kbuild.txt | 9 +++++++++
 Makefile                        | 3 +++
 2 files changed, 12 insertions(+)
Documentation/kbuild/kbuild.txt +0 -0
index ac2363ea05c5..3751a4bc8596 100644
@@ -24,6 +24,15 @@ KAFLAGS
 --------------------------------------------------
 Additional options to the assembler (for built-in and modules).

Documentation/kbuild/kbuild.txt KAFLAGS · line 35

Quoted 10 lines
Documentation/kbuild/kbuild.txt +9 -0
 
+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.

Quoted 24 lines
Documentation/kbuild/kbuild.txt +0 -0
 AFLAGS_MODULE
 --------------------------------------------------
 Additional module specific options to use for $(AS).
Makefile +3 -1
index 7ba478ab8c82..2cab3f8d489c 100644
@@ -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

Signature · 2 lines
Best Regards
Masahiro Yamada
#9 lore raw

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

Quoted 42 lines · Masahiro Yamada

2018-03-29 9:48 GMT+09:00 Laura Abbott <[email protected]>:

Quoted 18 lines

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.

---
  Documentation/kbuild/kbuild.txt | 9 +++++++++
  Makefile                        | 3 +++
  2 files changed, 12 insertions(+)
Documentation/kbuild/kbuild.txt +0 -0
index ac2363ea05c5..3751a4bc8596 100644
@@ -24,6 +24,15 @@ KAFLAGS
  --------------------------------------------------
  Additional options to the assembler (for built-in and modules).

Documentation/kbuild/kbuild.txt KAFLAGS · line 35

Quoted 10 lines
Documentation/kbuild/kbuild.txt +9 -0
 
+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.

Quoted 8 lines

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.

Quoted 30 lines
Quoted 27 lines
Documentation/kbuild/kbuild.txt +0 -0
  AFLAGS_MODULE
  --------------------------------------------------
  Additional module specific options to use for $(AS).
Makefile +3 -1
index 7ba478ab8c82..2cab3f8d489c 100644
@@ -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