Can other source code generate the same executable / binary?

Can other source code generate the same executable / binary?

Is it possible?

+3
source share
3 answers

Yes. Compilers can do many optimizations, and other source code can map to the same object code. Here are some trivial examples. Please note that they are language dependent.

  • You can express an integer in decimal, hexadecimal, octal or binary - the result in the object code will be the same.
  • In many languages, variable names are not displayed in the executable, and you can change the variable names without affecting the executable.
+6
source

Yes. for example

int nabs1(int a){ 
  return -abs(a); 
}

int nabs2(int a){ 
  return(a<0) ? a : -a;
}

gcc -O6 generates the same code:

 _nabs1:
pushl   %ebp
movl    %esp, %ebp
movl    8(%ebp), %edx
popl    %ebp
movl    %edx, %eax
sarl    $31, %eax
xorl    %eax, %edx
subl    %edx, %eax
ret
.p2align 4,,15
.globl _nabs2
.def    _nabs2; .scl    2;  .type   32; .endef
_nabs2:
pushl   %ebp
movl    %esp, %ebp
movl    8(%ebp), %edx
popl    %ebp
movl    %edx, %eax
sarl    $31, %eax
xorl    %eax, %edx
subl    %edx, %eax
ret
+4

It depends on how you define it. One could play with minor changes in Java or C # and generate them in one bytecode.

For example, in C, I expect that if I use the preprocess command for a literal string or use the literal string directly, then the generated code will be the same, but the source was different.

+1
source

Source: https://habr.com/ru/post/1717316/


All Articles