-5

I have the following C program:

#include<stdio.h>
int main()
{
    printf("hhhh");
    return 0;
}

Commands to compile, copy and compare:

$ gcc print.c -o a.out
$ objcopy a.out b.out
$ cmp a.out b.out

I have compiled this program and created an executable. Then, I have used the objcopy command to make a copy of the executable. But, when I compare these files, I get this:

files differ: byte 41, line 1

How can I know what contents are missing?
Any help or pointers would be appreciated. Thanks!

Azeem
  • 11,148
  • 4
  • 27
  • 40
Akshay
  • 159
  • 1
  • 9
  • Also you may want to specify Linux flavor you are using. I don't think this behavior is reproducible on all Linux. – g-217 Jan 23 '18 at 10:34
  • VTC because incomplete info: no identification of the compiler, OS/arch, what the difference is, etc. – underscore_d Jan 23 '18 at 10:38
  • Why do you think something is missing? It just says they differ. I suspect it's a timestamp. – Barmar Jan 23 '18 at 10:39
  • I am using cent OS yes this is not producible because I am testing it for the other compiler I am just want to know how we can find where the difference is ? – Akshay Jan 23 '18 at 10:42
  • 2
    Byte 41 in the 64 bit [ELF header](https://en.wikipedia.org/wiki/Executable_and_Linkable_Format) is a part of the section header offset field (`e_shoff`); in the 32 bit ELF header is a part of the header size field (`e_ehsize`). You should probably post a link to those files (or at least, a hex dump of the first 512 bytes or so) to let us help you. – Matteo Italia Jan 23 '18 at 10:45
  • 1
    Also, even just doing `cmp -l a.out b.out` and posting the output could prove valuable. – Matteo Italia Jan 23 '18 at 10:57
  • 2
    what are you trying to achieve? What is the actual problem? Does `a.b` have any issues? If you want an identical copy use `cp` instead. – bolov Jan 23 '18 at 11:54
  • when I take hexdump of two files and then find the difference then I found following difference can anyone please tell me know what it means -0002310 0008 0000 0000 0000 0000 0000 0000 0000 +0002310 0008 0000 0000 0000 0008 0000 0000 0000 – Akshay Jan 23 '18 at 12:35
  • Offset 0x2310 is way beyond the length of the binary generated with my gcc, so it's not in the initial header. Again, either you upload your binaries or there's no way we can help you. – Matteo Italia Jan 23 '18 at 13:06
  • @Matteo Italia how can I attach my hexdump files ? – Akshay Jan 23 '18 at 13:48
  • I found the difference. The difference is EntSize of .init_array section is 0 bytes in a.out file and it is 8 bytes in the b.out file can anyone know why this difference is coming ? – Akshay Jan 23 '18 at 14:35

1 Answers1

0

How can I know what contents are missing?

What made you believe that any contents is missing?

The way objcopy works is:

  1. parse the contents of the input file into internal representation.
  2. copy parts of the original file to the output file as instructed by options

Nowhere does objdump guarantee that when "copy entire file" is given, the result will be bit-identical.

In particular, non-loadable sections could be reordered or changed in other ways.

The difference is EntSize of .init_array section is 0 bytes in a.out file and it is 8 bytes in the b.out

The EntSize of 0 doesn't make sense for a non-empty section. If you really have such section in your a.out, it's likely that your linker has a bug.

Employed Russian
  • 199,314
  • 34
  • 295
  • 362