0

There are some functions which are never called. Not because they are not called under some logic but because they are never called from tree of main function. assuming:

int A()
{
   if(...)
      call F();
}

int B()
{
}

int C()
{
    call B();
}

int D()
{
    call A();
}

int F()
{
    call A();
}

int main()
{
    call D();
    call F();
}

so in this example:

main  ---> D
       |
       --> F

D  ------> A
F -------> A
A -------> F
C -------> B

So in no way by running this application B and C have chance of being called. They are orphan. But it seems gcc/g++ do not remove orphan functions as I have checked:

program 1:

int main()
{
    int a=4;
    int b=3;
    int c=a+b;
    b=c-a;
    a=c-b;
    return 0;
}

running in command line:

g++ -std=c++11 test.cpp
md5sum a.out

I get:

546da269abddb8dcb3883527a362f769  a.out

Now by adding an orpan function (test), I get different executive file:

program 2:

int test()
{

}

int main()
{
    int a=4;
    int b=3;
    int c=a+b;
    b=c-a;
    a=c-b;
    return 0;
}

running in command line:

g++ -std=c++11 test.cpp
md5sum a.out

gives different hash:

64095263965d2d94ed2f305f99a2b25a  a.out

It shows that this orphan function which will never be called in my program has influenced the compiled code. Is there any way to tell gcc/g++ to remove orphan functions?

nchen24
  • 502
  • 2
  • 9
barej
  • 1,330
  • 3
  • 25
  • 56
  • 1
    Merely recompiling the *exact* same source file will give a different hash too. GCC is not designed to always give the bit-for-bit same output file when you pass the same input file. Can you edit your question to include a more reliable test? –  Dec 11 '14 at 06:17
  • 1
    A function is never considered "orphaned" because its symbol is exported and available to other compilation units, which is something that gcc can't detect. – OmnipotentEntity Dec 11 '14 at 06:17
  • So, is there any source code level tool providing such facility to export my codes (excluding standard header files) to minimal code? – barej Dec 11 '14 at 06:23
  • I'm not sure I understand the point of this. Wouldn't it be better to just clean up your code by yourself? – nchen24 Dec 11 '14 at 06:25
  • @OmnipotentEntity what if our application is a final executive which is never used by any other application? – barej Dec 11 '14 at 06:26
  • @nchen24 imagine there is a pile of codes and you cannot do it by yourself as you write code incrementally during a long time. so, in real situation finding orphaned functions is not such easy and simple. – barej Dec 11 '14 at 06:28
  • 1
    @barej You can always use some terminal-fu like `nm ./a.out | c++filt | grep ' T ' | cut -f 3- -d' '` to get a list of the functions shown in the debug symbols, and grep for occurrences of those functions. – nchen24 Dec 11 '14 at 06:53

1 Answers1

1

When compiling use optimization option. The -O flag that is passed to the compiler

Change

g++ -std=c++11 test.cpp

TO

g++ -O1 -std=c++11 test.cpp

with this option the md5sum for both the program will be the same.

Program 1:

int main()
{
    int a=4;
    int b=3;
    int c=a+b;
    b=c-a;
    a=c-b;
    return 0;
}

and Program 2:

int test()
{

}

int main()
{
    int a=4;
    int b=3;
    int c=a+b;
    b=c-a;
    a=c-b;
    return 0;
}
cmaster - reinstate monica
  • 38,891
  • 9
  • 62
  • 106
Santosh A
  • 5,173
  • 27
  • 37