GCC Reproducible Builds: Transitioning from -frandom-seed to SOURCE_DATE_EPOCH
0 reputation · 17 Aug 2024, 22:47 UTC
0 reputation · 17 Aug 2024, 22:47 UTC
Achieving bit-for-bit identical binaries across different build environments in GCC requires neutralizing non-deterministic inputs. While -frandom-seed stabilizes optimization passes, the default expansion of __DATE__ and __TIME__ macros introduces temporal variance.
To maintain a repeatable development environment, GCC supports the SOURCE_DATE_EPOCH environment variable to override system timestamps. Additionally, absolute paths in DWARF debug information often cause binary divergence between different build machines, necessitating the use of -fdebug-prefix-map.
There is uncertainty regarding the interaction between multiple prefix maps and how they prioritize path replacements when overlapping patterns are defined in the configuration.
-fdebug-prefix-map flags target the same directory hierarchy?SOURCE_DATE_EPOCH fully supersede the need for specific macro overrides in all optimization levels?31775 reputation · 18 Aug 2024, 03:23 UTC
GCC does not define a portable precedence for overlapping -fdebug-prefix-map flags; the effective mapping depends on the GCC version and flag order, so you must test your specific toolchain. SOURCE_DATE_EPOCH fixes the expansion of __DATE__, __TIME__ and usually __TIMESTAMP__, but it does not affect __FILE__, absolute paths in DWARF, build‑id generation, LTO symbol naming or archive metadata, so macro‑level overrides may still be required.
The overlap handling evolved across GCC releases; early versions used first‑match, later ones switched to last‑match, and the implementation may consult flag order or internal hash tables, making the outcome non‑portable.
Setting SOURCE_DATE_EPOCH deterministically replaces the timestamps seen by the preprocessor, but it leaves other sources of nondeterminism untouched. Verified ways to eliminate those are -ffile-prefix-map, -fmacro-prefix-map for __FILE__ and general paths, -fdebug-prefix-map for DWARF, and a stable -frandom-seed when LTO or generated symbol names are present.
-fdebug-prefix-map entries, ordering them from most‑specific to most‑general.-ffile-prefix-map== and/or -fmacro-prefix-map if you want to hide __FILE__.SOURCE_DATE_EPOCH= before invoking GCC.-frandom-seed to a value derived deterministically from the input (e.g., a hash of the source file).readelf -w or dwarfdump on the output to verify that no absolute paths remain and that the intended prefix replacements appear.gcc -E -dM probe.c to confirm that __DATE__, __TIME__ and __TIMESTAMP__ expand to the fixed values.Missing diagnostic detail: whether your build enables link‑time optimization (LTO). Knowing this determines if you must retain a deterministic -frandom-seed in addition to SOURCE_DATE_EPOCH.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.