I've made an attempt at patching this issue as it produces unreproducible
unreproducible binaries for Golang. I don't know C/C++ and it's my first gcc
patch so please bear with me :)

-- >8 --

When -flto is used with line macros containing bare symbols instead of
absolute paths the full path is added but never remapped with
-ffile-prefix-map. This could produce unreproducible binaries.

Fixes: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108534

Signed-off-by: Morten Linderud <mor...@linderud.pw>
---
 gcc/lto-streamer-in.cc  | 6 ++++--
 gcc/lto-streamer-out.cc | 6 ++++--
 2 files changed, 8 insertions(+), 4 deletions(-)

diff --git a/gcc/lto-streamer-in.cc b/gcc/lto-streamer-in.cc
index ad0ca24007a..799f9eec478 100644
--- a/gcc/lto-streamer-in.cc
+++ b/gcc/lto-streamer-in.cc
@@ -44,6 +44,7 @@ along with GCC; see the file COPYING3.  If not see
 #include "debug.h"
 #include "alloc-pool.h"
 #include "toplev.h"
+#include "file-prefix-map.h" /* remap_debug_filename()  */
 
 /* Allocator used to hold string slot entries for line map streaming.  */
 static struct object_allocator<struct string_slot> *string_slot_allocator;
@@ -557,11 +558,12 @@ lto_location_cache::input_location_and_block (location_t 
*loc,
        {
          const char *pwd = bp_unpack_string (data_in, bp);
          const char *src_pwd = get_src_pwd ();
-         if (strcmp (pwd, src_pwd) == 0)
+         const char *remapped_src_pwd = remap_debug_filename (src_pwd);
+         if (strcmp (pwd, remapped_src_pwd) == 0)
            stream_relative_path_prefix = NULL;
          else
            stream_relative_path_prefix
-             = canon_relative_path_prefix (pwd, src_pwd);
+             = canon_relative_path_prefix (pwd, remapped_src_pwd);
        }
       stream_file = canon_file_name (stream_relative_path_prefix,
                                     bp_unpack_string (data_in, bp));
diff --git a/gcc/lto-streamer-out.cc b/gcc/lto-streamer-out.cc
index d4f728094ed..379e256a1e7 100644
--- a/gcc/lto-streamer-out.cc
+++ b/gcc/lto-streamer-out.cc
@@ -230,8 +230,10 @@ lto_output_location_1 (struct output_block *ob, struct 
bitpack_d *bp,
              ob->emit_pwd = false;
            }
          bp_pack_value (bp, stream_pwd, 1);
-         if (stream_pwd)
-           bp_pack_string (ob, bp, get_src_pwd (), true);
+         if (stream_pwd){
+           const char *remapped_pwd = remap_debug_filename (get_src_pwd ());
+           bp_pack_string (ob, bp, remapped_pwd, true);
+         }
          bp_pack_string (ob, bp, remapped, true);
          bp_pack_value (bp, xloc.sysp, 1);
        }
-- 
2.44.0

Reply via email to