C++: get the same binary after strip with and without "-g"

311 Views Asked by At

I'm trying to improve the build time on a 1GB shared library that's built from ~400 dependencies and then stripped to 100MB. The dependencies are not stripped, so I'm thinking it might build faster if I pre-strip the dependencies before (or simply build them without -g from the start). Testing this library is a big effort, but I can avoid testing if the new build process produces exactly the same binary.

I've made a small test for this, with just one C++ file, lib.cc:

int lib1_f1(int x) {
  return x + x;
}
int lib1_f2(int x) {
  return x*x;
}

And here's a Makefile:

diff: res1.so res2.so
    ./hexdump-diffuse $^

res1.so: lib.cc.o.1
    g++ -fPIC -shared -o $@ $^
    strip $@

res2.so: lib.cc.o.2
    g++ -fPIC -shared -o $@ $^
    strip $@

# with -g
%.cc.o.1: %.cc
    g++ -g -O2 -std=c++11 -c $^ -o $@

# without -g
%.cc.o.2: %.cc
    g++ -O2 -std=c++11 -c $^ -o $@

install-tools:
    sudo apt-get install diffuse bsdmainutils

.PHONY: install-tools diff

Here's a script I use for comparing binaries:

#!/bin/bash
hexdump -v -C $1 > /tmp/mydiff.1
hexdump -v -C $2 > /tmp/mydiff.2
diffuse /tmp/mydiff.1 /tmp/mydiff.2

The difference between the binaries I'm getting is around 10 bytes. Is there any way to avoid this difference, and have both methods produce identical binaries?

1

There are 1 best solutions below

0
On

Thanks to the hint, I ran this:

objdump -xD res1.so > res1.objdump
objdump -xD res2.so > res2.objdump
diffuse res1.objdump res2.objdump

Which lead me to using this for the strip command:

strip --remove-section=.note.gnu.build-id

Now the binaries are the same.