[PATCH v2] kbuild: make all file references relative to source root

Thomas Weißschuh posted 1 patch 9 months, 1 week ago
Documentation/kbuild/reproducible-builds.rst | 17 -----------------
Makefile                                     |  2 +-
arch/x86/boot/Makefile                       |  2 +-
arch/x86/boot/compressed/Makefile            |  2 +-
4 files changed, 3 insertions(+), 20 deletions(-)
[PATCH v2] kbuild: make all file references relative to source root
Posted by Thomas Weißschuh 9 months, 1 week ago
-fmacro-prefix-map only affects __FILE__ and __BASE_FILE__.
Other references, for example in debug information, are not affected.
This makes handling of file references in the compiler outputs harder to
use and creates problems for reproducible builds.

Switch to -ffile-prefix map which affects all references.

Also drop the documentation section advising manual specification of
-fdebug-prefix-map for reproducible builds, as it is not necessary
anymore.

Suggested-by: Ben Hutchings <ben@decadent.org.uk>
Link: https://lore.kernel.org/lkml/c49cc967294f9a3a4a34f69b6a8727a6d3959ed8.camel@decadent.org.uk/
Acked-by: Borislav Petkov (AMD) <bp@alien8.de> # arch/x86/
Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
---
Changes in v2:
- Pick up Ack from Borislav
- Merge all changes into single patch
- Also drop link to KCFLAGS from docs
- Link to v1: https://lore.kernel.org/r/20250313-kbuild-prefix-map-v1-0-38cea8448c5f@weissschuh.net
---
 Documentation/kbuild/reproducible-builds.rst | 17 -----------------
 Makefile                                     |  2 +-
 arch/x86/boot/Makefile                       |  2 +-
 arch/x86/boot/compressed/Makefile            |  2 +-
 4 files changed, 3 insertions(+), 20 deletions(-)

diff --git a/Documentation/kbuild/reproducible-builds.rst b/Documentation/kbuild/reproducible-builds.rst
index f2dcc39044e66ddd165646e0b51ccb0209aca7dd..a7762486c93fcd3eba08b836bed622a41e829e41 100644
--- a/Documentation/kbuild/reproducible-builds.rst
+++ b/Documentation/kbuild/reproducible-builds.rst
@@ -46,21 +46,6 @@ The kernel embeds the building user and host names in
 `KBUILD_BUILD_USER and KBUILD_BUILD_HOST`_ variables.  If you are
 building from a git commit, you could use its committer address.
 
-Absolute filenames
-------------------
-
-When the kernel is built out-of-tree, debug information may include
-absolute filenames for the source files.  This must be overridden by
-including the ``-fdebug-prefix-map`` option in the `KCFLAGS`_ variable.
-
-Depending on the compiler used, the ``__FILE__`` macro may also expand
-to an absolute filename in an out-of-tree build.  Kbuild automatically
-uses the ``-fmacro-prefix-map`` option to prevent this, if it is
-supported.
-
-The Reproducible Builds web site has more information about these
-`prefix-map options`_.
-
 Generated files in source packages
 ----------------------------------
 
@@ -131,7 +116,5 @@ See ``scripts/setlocalversion`` for details.
 
 .. _KBUILD_BUILD_TIMESTAMP: kbuild.html#kbuild-build-timestamp
 .. _KBUILD_BUILD_USER and KBUILD_BUILD_HOST: kbuild.html#kbuild-build-user-kbuild-build-host
-.. _KCFLAGS: kbuild.html#kcflags
-.. _prefix-map options: https://reproducible-builds.org/docs/build-path/
 .. _Reproducible Builds project: https://reproducible-builds.org/
 .. _SOURCE_DATE_EPOCH: https://reproducible-builds.org/docs/source-date-epoch/
diff --git a/Makefile b/Makefile
index 5c333682dc9142b1aacfe454a5c77f5923554b7d..4f920187cee658ae4d1b807fca365f6994274828 100644
--- a/Makefile
+++ b/Makefile
@@ -1067,7 +1067,7 @@ endif
 
 # change __FILE__ to the relative path to the source directory
 ifdef building_out_of_srctree
-KBUILD_CPPFLAGS += $(call cc-option,-fmacro-prefix-map=$(srcroot)/=)
+KBUILD_CPPFLAGS += $(call cc-option,-ffile-prefix-map=$(srcroot)/=)
 KBUILD_RUSTFLAGS += --remap-path-prefix=$(srcroot)/=
 endif
 
diff --git a/arch/x86/boot/Makefile b/arch/x86/boot/Makefile
index 9cc0ff6e9067d574488a35573eff4d0f8449e398..f500f82864aae80deb74faa3df9a8b6333d6c4ca 100644
--- a/arch/x86/boot/Makefile
+++ b/arch/x86/boot/Makefile
@@ -54,7 +54,7 @@ targets += cpustr.h
 
 KBUILD_CFLAGS	:= $(REALMODE_CFLAGS) -D_SETUP
 KBUILD_AFLAGS	:= $(KBUILD_CFLAGS) -D__ASSEMBLY__
-KBUILD_CFLAGS	+= $(call cc-option,-fmacro-prefix-map=$(srctree)/=)
+KBUILD_CFLAGS	+= $(call cc-option,-ffile-prefix-map=$(srctree)/=)
 KBUILD_CFLAGS	+= -fno-asynchronous-unwind-tables
 KBUILD_CFLAGS	+= $(CONFIG_CC_IMPLICIT_FALLTHROUGH)
 
diff --git a/arch/x86/boot/compressed/Makefile b/arch/x86/boot/compressed/Makefile
index 5edee7a9786c67e13c295473751b82387bcbd67e..ad324978b2e5b1b6f8be82647769c99db8257ac7 100644
--- a/arch/x86/boot/compressed/Makefile
+++ b/arch/x86/boot/compressed/Makefile
@@ -38,7 +38,7 @@ KBUILD_CFLAGS += -fno-stack-protector
 KBUILD_CFLAGS += $(call cc-disable-warning, address-of-packed-member)
 KBUILD_CFLAGS += $(call cc-disable-warning, gnu)
 KBUILD_CFLAGS += -Wno-pointer-sign
-KBUILD_CFLAGS += $(call cc-option,-fmacro-prefix-map=$(srctree)/=)
+KBUILD_CFLAGS += $(call cc-option,-ffile-prefix-map=$(srctree)/=)
 KBUILD_CFLAGS += -fno-asynchronous-unwind-tables
 KBUILD_CFLAGS += -D__DISABLE_EXPORTS
 # Disable relocation relaxation in case the link is not PIE.

---
base-commit: a57512d6cd88eba04cdc1fb83832c00d248bd0d1
change-id: 20250312-kbuild-prefix-map-5ae76c209e7a

Best regards,
-- 
Thomas Weißschuh <linux@weissschuh.net>

Re: [PATCH v2] kbuild: make all file references relative to source root
Posted by Ville Syrjälä 7 months, 3 weeks ago
On Sat, Mar 15, 2025 at 02:20:14PM +0100, Thomas Weißschuh wrote:
> -fmacro-prefix-map only affects __FILE__ and __BASE_FILE__.
> Other references, for example in debug information, are not affected.
> This makes handling of file references in the compiler outputs harder to
> use and creates problems for reproducible builds.
> 
> Switch to -ffile-prefix map which affects all references.
> 
> Also drop the documentation section advising manual specification of
> -fdebug-prefix-map for reproducible builds, as it is not necessary
> anymore.

Hi,

This broke 'objdump -S' completely for me.

I see the following difference in the debug info:
-    <12>   DW_AT_name        : (indirect line string, offset: 0): drivers/gpu/drm/i915/i915_config.c
-    <16>   DW_AT_comp_dir    : (indirect line string, offset: 0x23): /home/.../src/linux-2.6/build
+    <12>   DW_AT_name        : (indirect line string, offset: 0): ../drivers/gpu/drm/i915/i915_config.c
+    <16>   DW_AT_comp_dir    : (indirect line string, offset: 0x26): /home/.../src/linux-2.6/build

Looks like I can work around it with some combination of --prefix and
--prefix-strip, but that seems far too tedious to have to do every
time I need to decode an oops.

> 
> Suggested-by: Ben Hutchings <ben@decadent.org.uk>
> Link: https://lore.kernel.org/lkml/c49cc967294f9a3a4a34f69b6a8727a6d3959ed8.camel@decadent.org.uk/
> Acked-by: Borislav Petkov (AMD) <bp@alien8.de> # arch/x86/
> Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
> ---
> Changes in v2:
> - Pick up Ack from Borislav
> - Merge all changes into single patch
> - Also drop link to KCFLAGS from docs
> - Link to v1: https://lore.kernel.org/r/20250313-kbuild-prefix-map-v1-0-38cea8448c5f@weissschuh.net
> ---
>  Documentation/kbuild/reproducible-builds.rst | 17 -----------------
>  Makefile                                     |  2 +-
>  arch/x86/boot/Makefile                       |  2 +-
>  arch/x86/boot/compressed/Makefile            |  2 +-
>  4 files changed, 3 insertions(+), 20 deletions(-)
> 
> diff --git a/Documentation/kbuild/reproducible-builds.rst b/Documentation/kbuild/reproducible-builds.rst
> index f2dcc39044e66ddd165646e0b51ccb0209aca7dd..a7762486c93fcd3eba08b836bed622a41e829e41 100644
> --- a/Documentation/kbuild/reproducible-builds.rst
> +++ b/Documentation/kbuild/reproducible-builds.rst
> @@ -46,21 +46,6 @@ The kernel embeds the building user and host names in
>  `KBUILD_BUILD_USER and KBUILD_BUILD_HOST`_ variables.  If you are
>  building from a git commit, you could use its committer address.
>  
> -Absolute filenames
> -------------------
> -
> -When the kernel is built out-of-tree, debug information may include
> -absolute filenames for the source files.  This must be overridden by
> -including the ``-fdebug-prefix-map`` option in the `KCFLAGS`_ variable.
> -
> -Depending on the compiler used, the ``__FILE__`` macro may also expand
> -to an absolute filename in an out-of-tree build.  Kbuild automatically
> -uses the ``-fmacro-prefix-map`` option to prevent this, if it is
> -supported.
> -
> -The Reproducible Builds web site has more information about these
> -`prefix-map options`_.
> -
>  Generated files in source packages
>  ----------------------------------
>  
> @@ -131,7 +116,5 @@ See ``scripts/setlocalversion`` for details.
>  
>  .. _KBUILD_BUILD_TIMESTAMP: kbuild.html#kbuild-build-timestamp
>  .. _KBUILD_BUILD_USER and KBUILD_BUILD_HOST: kbuild.html#kbuild-build-user-kbuild-build-host
> -.. _KCFLAGS: kbuild.html#kcflags
> -.. _prefix-map options: https://reproducible-builds.org/docs/build-path/
>  .. _Reproducible Builds project: https://reproducible-builds.org/
>  .. _SOURCE_DATE_EPOCH: https://reproducible-builds.org/docs/source-date-epoch/
> diff --git a/Makefile b/Makefile
> index 5c333682dc9142b1aacfe454a5c77f5923554b7d..4f920187cee658ae4d1b807fca365f6994274828 100644
> --- a/Makefile
> +++ b/Makefile
> @@ -1067,7 +1067,7 @@ endif
>  
>  # change __FILE__ to the relative path to the source directory
>  ifdef building_out_of_srctree
> -KBUILD_CPPFLAGS += $(call cc-option,-fmacro-prefix-map=$(srcroot)/=)
> +KBUILD_CPPFLAGS += $(call cc-option,-ffile-prefix-map=$(srcroot)/=)
>  KBUILD_RUSTFLAGS += --remap-path-prefix=$(srcroot)/=
>  endif
>  
> diff --git a/arch/x86/boot/Makefile b/arch/x86/boot/Makefile
> index 9cc0ff6e9067d574488a35573eff4d0f8449e398..f500f82864aae80deb74faa3df9a8b6333d6c4ca 100644
> --- a/arch/x86/boot/Makefile
> +++ b/arch/x86/boot/Makefile
> @@ -54,7 +54,7 @@ targets += cpustr.h
>  
>  KBUILD_CFLAGS	:= $(REALMODE_CFLAGS) -D_SETUP
>  KBUILD_AFLAGS	:= $(KBUILD_CFLAGS) -D__ASSEMBLY__
> -KBUILD_CFLAGS	+= $(call cc-option,-fmacro-prefix-map=$(srctree)/=)
> +KBUILD_CFLAGS	+= $(call cc-option,-ffile-prefix-map=$(srctree)/=)
>  KBUILD_CFLAGS	+= -fno-asynchronous-unwind-tables
>  KBUILD_CFLAGS	+= $(CONFIG_CC_IMPLICIT_FALLTHROUGH)
>  
> diff --git a/arch/x86/boot/compressed/Makefile b/arch/x86/boot/compressed/Makefile
> index 5edee7a9786c67e13c295473751b82387bcbd67e..ad324978b2e5b1b6f8be82647769c99db8257ac7 100644
> --- a/arch/x86/boot/compressed/Makefile
> +++ b/arch/x86/boot/compressed/Makefile
> @@ -38,7 +38,7 @@ KBUILD_CFLAGS += -fno-stack-protector
>  KBUILD_CFLAGS += $(call cc-disable-warning, address-of-packed-member)
>  KBUILD_CFLAGS += $(call cc-disable-warning, gnu)
>  KBUILD_CFLAGS += -Wno-pointer-sign
> -KBUILD_CFLAGS += $(call cc-option,-fmacro-prefix-map=$(srctree)/=)
> +KBUILD_CFLAGS += $(call cc-option,-ffile-prefix-map=$(srctree)/=)
>  KBUILD_CFLAGS += -fno-asynchronous-unwind-tables
>  KBUILD_CFLAGS += -D__DISABLE_EXPORTS
>  # Disable relocation relaxation in case the link is not PIE.
> 
> ---
> base-commit: a57512d6cd88eba04cdc1fb83832c00d248bd0d1
> change-id: 20250312-kbuild-prefix-map-5ae76c209e7a
> 
> Best regards,
> -- 
> Thomas Weißschuh <linux@weissschuh.net>
> 

-- 
Ville Syrjälä
Intel
Re: [PATCH v2] kbuild: make all file references relative to source root
Posted by Thomas Weißschuh 7 months, 2 weeks ago
On 2025-04-29 22:51:17+0300, Ville Syrjälä wrote:
> On Sat, Mar 15, 2025 at 02:20:14PM +0100, Thomas Weißschuh wrote:
> > -fmacro-prefix-map only affects __FILE__ and __BASE_FILE__.
> > Other references, for example in debug information, are not affected.
> > This makes handling of file references in the compiler outputs harder to
> > use and creates problems for reproducible builds.
> > 
> > Switch to -ffile-prefix map which affects all references.
> > 
> > Also drop the documentation section advising manual specification of
> > -fdebug-prefix-map for reproducible builds, as it is not necessary
> > anymore.
> 
> Hi,
> 
> This broke 'objdump -S' completely for me.
> 
> I see the following difference in the debug info:
> -    <12>   DW_AT_name        : (indirect line string, offset: 0): drivers/gpu/drm/i915/i915_config.c
> -    <16>   DW_AT_comp_dir    : (indirect line string, offset: 0x23): /home/.../src/linux-2.6/build
> +    <12>   DW_AT_name        : (indirect line string, offset: 0): ../drivers/gpu/drm/i915/i915_config.c
> +    <16>   DW_AT_comp_dir    : (indirect line string, offset: 0x26): /home/.../src/linux-2.6/build
> 
> Looks like I can work around it with some combination of --prefix and
> --prefix-strip, but that seems far too tedious to have to do every
> time I need to decode an oops.

Yeah the --prefix and --prefix-strip mechanism is only an ugly
workaround.
Unfortunately I don't see a nice for this issue at the moment.

Masahiro, could you revert this commit for now?
I'll try to come up with something better.


Thomas
Re: [PATCH v2] kbuild: make all file references relative to source root
Posted by Masahiro Yamada 7 months, 1 week ago
On Mon, May 5, 2025 at 9:30 AM Thomas Weißschuh <linux@weissschuh.net> wrote:
>
> On 2025-04-29 22:51:17+0300, Ville Syrjälä wrote:
> > On Sat, Mar 15, 2025 at 02:20:14PM +0100, Thomas Weißschuh wrote:
> > > -fmacro-prefix-map only affects __FILE__ and __BASE_FILE__.
> > > Other references, for example in debug information, are not affected.
> > > This makes handling of file references in the compiler outputs harder to
> > > use and creates problems for reproducible builds.
> > >
> > > Switch to -ffile-prefix map which affects all references.
> > >
> > > Also drop the documentation section advising manual specification of
> > > -fdebug-prefix-map for reproducible builds, as it is not necessary
> > > anymore.
> >
> > Hi,
> >
> > This broke 'objdump -S' completely for me.
> >
> > I see the following difference in the debug info:
> > -    <12>   DW_AT_name        : (indirect line string, offset: 0): drivers/gpu/drm/i915/i915_config.c
> > -    <16>   DW_AT_comp_dir    : (indirect line string, offset: 0x23): /home/.../src/linux-2.6/build
> > +    <12>   DW_AT_name        : (indirect line string, offset: 0): ../drivers/gpu/drm/i915/i915_config.c
> > +    <16>   DW_AT_comp_dir    : (indirect line string, offset: 0x26): /home/.../src/linux-2.6/build
> >
> > Looks like I can work around it with some combination of --prefix and
> > --prefix-strip, but that seems far too tedious to have to do every
> > time I need to decode an oops.
>
> Yeah the --prefix and --prefix-strip mechanism is only an ugly
> workaround.
> Unfortunately I don't see a nice for this issue at the moment.
>
> Masahiro, could you revert this commit for now?
> I'll try to come up with something better.

Could you submit a patch
with a commit description?

Thanks.




-- 
Best Regards
Masahiro Yamada
Re: [PATCH v2] kbuild: make all file references relative to source root
Posted by Matthieu Baerts 7 months, 3 weeks ago
Hi Thomas, Masahiro,

+Cc Peter Oberparleiter, MPTCP ML

On 15/03/2025 14:20, Thomas Weißschuh wrote:
> -fmacro-prefix-map only affects __FILE__ and __BASE_FILE__.
> Other references, for example in debug information, are not affected.
> This makes handling of file references in the compiler outputs harder to
> use and creates problems for reproducible builds.
> 
> Switch to -ffile-prefix map which affects all references.
> 
> Also drop the documentation section advising manual specification of
> -fdebug-prefix-map for reproducible builds, as it is not necessary
> anymore.
> 
> Suggested-by: Ben Hutchings <ben@decadent.org.uk>
> Link: https://lore.kernel.org/lkml/c49cc967294f9a3a4a34f69b6a8727a6d3959ed8.camel@decadent.org.uk/
> Acked-by: Borislav Petkov (AMD) <bp@alien8.de> # arch/x86/
> Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>

Thank you for having worked on that!

(...)

> diff --git a/Makefile b/Makefile
> index 5c333682dc9142b1aacfe454a5c77f5923554b7d..4f920187cee658ae4d1b807fca365f6994274828 100644
> --- a/Makefile
> +++ b/Makefile
> @@ -1067,7 +1067,7 @@ endif
>  
>  # change __FILE__ to the relative path to the source directory
>  ifdef building_out_of_srctree
> -KBUILD_CPPFLAGS += $(call cc-option,-fmacro-prefix-map=$(srcroot)/=)
> +KBUILD_CPPFLAGS += $(call cc-option,-ffile-prefix-map=$(srcroot)/=)
>  KBUILD_RUSTFLAGS += --remap-path-prefix=$(srcroot)/=
>  endif

Today, I noticed that my CI for the MPTCP subsystem couldn't produce
code coverage files like before: the source files are not found. A 'git
bisect' pointed me to this patch. Reverting it seems to fix the issue.


My CI is building the kernel out of the source tree, in ".virtme/build".
Before and after this patch, GCOV seems to do its job properly.
Capturing GCOV data with this lcov command seems OK too:

  lcov --capture --keep-going -j "${INPUT_CPUS}" \
     --rc geninfo_unexecuted_blocks=1 \
     --include '/net/mptcp/' \
     --function-coverage --branch-coverage \
     -b "${PWD}/.virtme/build" -o kernel.lcov

But after this patch, lcov complains some files are not found, e.g.

  ERROR: (source) unable to open
${WORKDIR}/.virtme/build/net/mptcp/ctrl.c: No such file or directory


The output file is different: the path to the source file is wrong
because it points to the build dir. Instead of ...

  SF:${WORKDIR}/net/mptcp/ctrl.c

... now I have ...

  SF:${WORKDIR}/.virtme/build/net/mptcp/ctrl.c


Are there modifications needed on GCOV side to adapt to the behaviour
change introduced by this patch? Or something else needed on the
userspace side?

Cheers,
Matt
-- 
Sponsored by the NGI0 Core fund.

Re: [PATCH v2] kbuild: make all file references relative to source root
Posted by Peter Oberparleiter 7 months, 2 weeks ago
On 29.04.2025 18:12, Matthieu Baerts wrote:
> On 15/03/2025 14:20, Thomas Weißschuh wrote:
>> -fmacro-prefix-map only affects __FILE__ and __BASE_FILE__.
>> Other references, for example in debug information, are not affected.
>> This makes handling of file references in the compiler outputs harder to
>> use and creates problems for reproducible builds.
>>
>> Switch to -ffile-prefix map which affects all references.
>>
>> Also drop the documentation section advising manual specification of
>> -fdebug-prefix-map for reproducible builds, as it is not necessary
>> anymore.
>>
>> Suggested-by: Ben Hutchings <ben@decadent.org.uk>
>> Link: https://lore.kernel.org/lkml/c49cc967294f9a3a4a34f69b6a8727a6d3959ed8.camel@decadent.org.uk/
>> Acked-by: Borislav Petkov (AMD) <bp@alien8.de> # arch/x86/
>> Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
> 
> Thank you for having worked on that!
> 
> (...)
> 
>> diff --git a/Makefile b/Makefile
>> index 5c333682dc9142b1aacfe454a5c77f5923554b7d..4f920187cee658ae4d1b807fca365f6994274828 100644
>> --- a/Makefile
>> +++ b/Makefile
>> @@ -1067,7 +1067,7 @@ endif
>>  
>>  # change __FILE__ to the relative path to the source directory
>>  ifdef building_out_of_srctree
>> -KBUILD_CPPFLAGS += $(call cc-option,-fmacro-prefix-map=$(srcroot)/=)
>> +KBUILD_CPPFLAGS += $(call cc-option,-ffile-prefix-map=$(srcroot)/=)
>>  KBUILD_RUSTFLAGS += --remap-path-prefix=$(srcroot)/=
>>  endif
> 
> Today, I noticed that my CI for the MPTCP subsystem couldn't produce
> code coverage files like before: the source files are not found. A 'git
> bisect' pointed me to this patch. Reverting it seems to fix the issue.
> 
> 
> My CI is building the kernel out of the source tree, in ".virtme/build".
> Before and after this patch, GCOV seems to do its job properly.
> Capturing GCOV data with this lcov command seems OK too:
> 
>   lcov --capture --keep-going -j "${INPUT_CPUS}" \
>      --rc geninfo_unexecuted_blocks=1 \
>      --include '/net/mptcp/' \
>      --function-coverage --branch-coverage \
>      -b "${PWD}/.virtme/build" -o kernel.lcov
> 
> But after this patch, lcov complains some files are not found, e.g.
> 
>   ERROR: (source) unable to open
> ${WORKDIR}/.virtme/build/net/mptcp/ctrl.c: No such file or directory

I can confirm that the subject kernel commit breaks gcov-kernel for
out-of-srctree builds by making it impossible for a consumer of GCOV
data to determine the actual location of a source file without manually
specifying it.

Sample .gcno file content changes as seen with gcov-dump:

- cwd: /home/.../build
- /home/.../linux/kernel/workqueue.c:8049:19-8057:1
+ cwd: /home/.../build
+ kernel/workqueue.c:8049:19-8057:1

> The output file is different: the path to the source file is wrong
> because it points to the build dir. Instead of ...
> 
>   SF:${WORKDIR}/net/mptcp/ctrl.c
> 
> ... now I have ...
> 
>   SF:${WORKDIR}/.virtme/build/net/mptcp/ctrl.c
> 
> 
> Are there modifications needed on GCOV side to adapt to the behaviour
> change introduced by this patch? Or something else needed on the
> userspace side?

I don't see how this could be fixed by changes in userspace nor
gcov-kernel - the source tree directory information is missing from the
relevant data files.

-- 
Peter Oberparleiter
Linux on IBM Z Development - IBM Germany R&D
Re: [PATCH v2] kbuild: make all file references relative to source root
Posted by Thomas Weißschuh 7 months, 2 weeks ago
Hi Matthieu,

On 2025-04-29 18:12:29+0200, Matthieu Baerts wrote:
> Hi Thomas, Masahiro,
> 
> +Cc Peter Oberparleiter, MPTCP ML
> 
> On 15/03/2025 14:20, Thomas Weißschuh wrote:
> > -fmacro-prefix-map only affects __FILE__ and __BASE_FILE__.
> > Other references, for example in debug information, are not affected.
> > This makes handling of file references in the compiler outputs harder to
> > use and creates problems for reproducible builds.
> > 
> > Switch to -ffile-prefix map which affects all references.
> > 
> > Also drop the documentation section advising manual specification of
> > -fdebug-prefix-map for reproducible builds, as it is not necessary
> > anymore.
> > 
> > Suggested-by: Ben Hutchings <ben@decadent.org.uk>
> > Link: https://lore.kernel.org/lkml/c49cc967294f9a3a4a34f69b6a8727a6d3959ed8.camel@decadent.org.uk/
> > Acked-by: Borislav Petkov (AMD) <bp@alien8.de> # arch/x86/
> > Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
> 
> Thank you for having worked on that!
> 
> (...)
> 
> > diff --git a/Makefile b/Makefile
> > index 5c333682dc9142b1aacfe454a5c77f5923554b7d..4f920187cee658ae4d1b807fca365f6994274828 100644
> > --- a/Makefile
> > +++ b/Makefile
> > @@ -1067,7 +1067,7 @@ endif
> >  
> >  # change __FILE__ to the relative path to the source directory
> >  ifdef building_out_of_srctree
> > -KBUILD_CPPFLAGS += $(call cc-option,-fmacro-prefix-map=$(srcroot)/=)
> > +KBUILD_CPPFLAGS += $(call cc-option,-ffile-prefix-map=$(srcroot)/=)
> >  KBUILD_RUSTFLAGS += --remap-path-prefix=$(srcroot)/=
> >  endif
> 
> Today, I noticed that my CI for the MPTCP subsystem couldn't produce
> code coverage files like before: the source files are not found. A 'git
> bisect' pointed me to this patch. Reverting it seems to fix the issue.
> 
> 
> My CI is building the kernel out of the source tree, in ".virtme/build".
> Before and after this patch, GCOV seems to do its job properly.
> Capturing GCOV data with this lcov command seems OK too:
> 
>   lcov --capture --keep-going -j "${INPUT_CPUS}" \
>      --rc geninfo_unexecuted_blocks=1 \
>      --include '/net/mptcp/' \
>      --function-coverage --branch-coverage \
>      -b "${PWD}/.virtme/build" -o kernel.lcov
> 
> But after this patch, lcov complains some files are not found, e.g.
> 
>   ERROR: (source) unable to open
> ${WORKDIR}/.virtme/build/net/mptcp/ctrl.c: No such file or directory
> 
> 
> The output file is different: the path to the source file is wrong
> because it points to the build dir. Instead of ...
> 
>   SF:${WORKDIR}/net/mptcp/ctrl.c
> 
> ... now I have ...
> 
>   SF:${WORKDIR}/.virtme/build/net/mptcp/ctrl.c
> 
> 
> Are there modifications needed on GCOV side to adapt to the behaviour
> change introduced by this patch? Or something else needed on the
> userspace side?

I'd like to focus on the report from Ville Syrjälä [0] first.
While it is easier to reproduce and investigate, I suspect the solution
found there will also work for your usecase.


Thomas

[0] https://lore.kernel.org/lkml/aBEttQH4kimHFScx@intel.com/
Re: [PATCH v2] kbuild: make all file references relative to source root
Posted by Ben Hutchings 9 months ago
On Sat, 2025-03-15 at 14:20 +0100, Thomas Weißschuh wrote:
> -fmacro-prefix-map only affects __FILE__ and __BASE_FILE__.
> Other references, for example in debug information, are not affected.
> This makes handling of file references in the compiler outputs harder to
> use and creates problems for reproducible builds.
> 
> Switch to -ffile-prefix map which affects all references.

This appears to cover all the C sources, but not quite all the assembly
sources:

[...]

> --- a/arch/x86/boot/Makefile
> +++ b/arch/x86/boot/Makefile
> @@ -54,7 +54,7 @@ targets += cpustr.h
>  
>  KBUILD_CFLAGS	:= $(REALMODE_CFLAGS) -D_SETUP
>  KBUILD_AFLAGS	:= $(KBUILD_CFLAGS) -D__ASSEMBLY__
> -KBUILD_CFLAGS	+= $(call cc-option,-fmacro-prefix-map=$(srctree)/=)
> +KBUILD_CFLAGS	+= $(call cc-option,-ffile-prefix-map=$(srctree)/=)
[...]

I think this addition to KBUILD_CFLAGS needs to be done before the
assignment to KBUILD_AFLAGS.

Also, in some older versions of gcc the -ffile-prefix-map option didn't
affect assembly sources - gas only understands --debug-prefix-map and
gcc did not do the necessary conversion.  But this works properly since
at least gcc 12, so I wouldn't worry too much about it.

Ben.

-- 
Ben Hutchings
Lowery's Law:
        If it jams, force it. If it breaks, it needed replacing anyway.
Re: [PATCH v2] kbuild: make all file references relative to source root
Posted by Nathan Chancellor 9 months ago
On Sun, Mar 16, 2025 at 08:08:37PM +0100, Ben Hutchings wrote:
> On Sat, 2025-03-15 at 14:20 +0100, Thomas Weißschuh wrote:
> > -fmacro-prefix-map only affects __FILE__ and __BASE_FILE__.
> > Other references, for example in debug information, are not affected.
> > This makes handling of file references in the compiler outputs harder to
> > use and creates problems for reproducible builds.
> > 
> > Switch to -ffile-prefix map which affects all references.
> 
> This appears to cover all the C sources, but not quite all the assembly
> sources:
> 
> [...]
> 
> > --- a/arch/x86/boot/Makefile
> > +++ b/arch/x86/boot/Makefile
> > @@ -54,7 +54,7 @@ targets += cpustr.h
> >  
> >  KBUILD_CFLAGS	:= $(REALMODE_CFLAGS) -D_SETUP
> >  KBUILD_AFLAGS	:= $(KBUILD_CFLAGS) -D__ASSEMBLY__
> > -KBUILD_CFLAGS	+= $(call cc-option,-fmacro-prefix-map=$(srctree)/=)
> > +KBUILD_CFLAGS	+= $(call cc-option,-ffile-prefix-map=$(srctree)/=)
> [...]
> 
> I think this addition to KBUILD_CFLAGS needs to be done before the
> assignment to KBUILD_AFLAGS.

This probably belongs in KBUILD_CPPFLAGS then, similar to clang's
'--target' flag and other options that we always want invoked?

Cheers,
Nathan
Re: [PATCH v2] kbuild: make all file references relative to source root
Posted by Masahiro Yamada 9 months ago
On Sat, Mar 15, 2025 at 10:20 PM Thomas Weißschuh <linux@weissschuh.net> wrote:
>
> -fmacro-prefix-map only affects __FILE__ and __BASE_FILE__.
> Other references, for example in debug information, are not affected.
> This makes handling of file references in the compiler outputs harder to
> use and creates problems for reproducible builds.
>
> Switch to -ffile-prefix map which affects all references.
>
> Also drop the documentation section advising manual specification of
> -fdebug-prefix-map for reproducible builds, as it is not necessary
> anymore.
>
> Suggested-by: Ben Hutchings <ben@decadent.org.uk>
> Link: https://lore.kernel.org/lkml/c49cc967294f9a3a4a34f69b6a8727a6d3959ed8.camel@decadent.org.uk/
> Acked-by: Borislav Petkov (AMD) <bp@alien8.de> # arch/x86/
> Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
> ---
> Changes in v2:
> - Pick up Ack from Borislav
> - Merge all changes into single patch
> - Also drop link to KCFLAGS from docs
> - Link to v1: https://lore.kernel.org/r/20250313-kbuild-prefix-map-v1-0-38cea8448c5f@weissschuh.net
> ---

Applied to linux-kbuild. Thanks.


--
Best Regards
Masahiro Yamada