Clang and -frandom-seed

Dimitry Andric dim at FreeBSD.org
Sun Nov 14 23:10:02 UTC 2010


On 2010-11-14 23:29, Erik Cederstrand wrote:
> I noticed that two consecutive builds of (GCC-built) Clang don't produce identical binaries. This is true for clang, clang++ and tblgen. I asked on the llvm-dev list yesterday, and it turns out it's because GCC uses a random seed on some symbols. Apparently, this can be controlled with the -frandom-seed flag. I haven't tested if this is also the case for Clang-built Clang.
>
> I'm not sure I understand the exact implications, but I'm wondering if we could add the flag to the build scripts in FreeBSD in a way that both satisfies the randomness criteria and makes builds deterministic?

Hmm, it seems this is only used for C++ compilations, not for plain C.
The gcc manual says:

-frandom-seed=string
     This option provides a seed that GCC uses when it would otherwise
     use random numbers. It is used to generate certain symbol names
     that have to be different in every compiled file.  It is also used
     to place unique stamps in coverage data files and the object files
     that produce them. You can use the -frandom-seed option to produce
     reproducibly identical object files.

     The string should be different for every file you compile.

The stanza "have to be different in every compiled file" is what worries
me.  There is probably a good reason that gcc needs to be able to emit
unique function names. In contrib/gcc/tree.c, there's this piece of
code:

/* Generate a name for a function unique to this translation unit.
    TYPE is some string to identify the purpose of this function to the
    linker or collect2.  */

tree
get_file_function_name_long (const char *type)
{
...
       /* We don't have anything that we know to be unique to this translation
          unit, so use what we do have and throw in some randomness.  */
...
       sprintf (q + len, "_%08X_%08X", crc32_string (0, name),
                crc32_string (0, flag_random_seed));

So this is all on purpose, and I think it would be a bad idea to disable
it, unless we fully understand the consequences.

On the other hand, the requirement "The string should be different for
every file you compile", could possibly be fulfilled.  Maybe by using
the filename, relative to $SRCDIR, that is being compiled as "seed"?

This would be unique for each compiled file, but still give the same
result for each build, and also be independent of the particular machine
you are building on.


More information about the freebsd-toolchain mailing list