Why don't two binaries of programs with only comments changed exactly match in gcc?

后端 未结 3 1907
悲&欢浪女
悲&欢浪女 2020-12-23 12:51

I created two C programs

  1. Program 1

    int main()
    {
    }
    
  2. Program 2

    int main()
    {
    //Some Harmless comments
             
    
    
            
3条回答
  •  难免孤独
    2020-12-23 13:35

    It's because the file names are different (although the strings output is the same). If you try modifying the file itself (rather than having two files), you'll notice that the output binaries are no longer different. As both Jens and I said, it's because GCC dumps a whole load of metadata into the binaries it builds, including the exact source filename (and AFAICS so does clang).

    Try this:

    $ cp code.c code2.c subdir/code.c
    $ gcc code.c -o a
    $ gcc code2.c -o b
    $ gcc subdir/code.c -o a2
    $ diff a b
    Binary files a and b differ
    $ diff a2 b
    Binary files a2 and b differ
    $ diff -s a a2
    Files a and a2 are identical
    

    This explains why your md5sums don't change between builds, but they are different between different files. If you want, you can do what Jens suggested and compare the output of strings for each binary you'll notice that the filenames are embedded in the binary. If you want to "fix" this, you can strip the binaries and the metadata will be removed:

    $ strip a a2 b
    $ diff -s a b
    Files a and b are identical
    $ diff -s a2 b
    Files a2 and b are identical
    $ diff -s a a2
    Files a and a2 are identical
    

提交回复
热议问题