Solaris Studio adding current directory info in object file

旧时模样 提交于 2019-12-12 03:26:25

问题


I am trying to build a simple hello world program using Solaris Studio 12.x on Solaris 11. If I compile the same file from two different folder, I am getting a difference in the object file as shown by diff command or cmp command.

Took a hex dump of both object files (od -x) and compared them and using an online tool that can convert hex to ASCII, found that the difference is occurring as the object files contain the current directory from where the compilation was initiated.

Question is how to tell compiler not to include the information on current directory in the object files. Tried few optimizaton options - was not useful to resolve this.

Here are the codes and commands that I used:

bash-4.1$ cat a.cpp
#include <stdio.h>

int main() {
  printf("Hello World!");
  return 0;

}
bash-4.1$ ls
a.cpp  temp1  temp2
bash-4.1$ cd temp1
bash-4.1$ /opt/SunProd/studio12u3/solarisstudio12.3//bin/CC ../a.cpp
bash-4.1$ cd ../temp2
bash-4.1$ /opt/SunProd/studio12u3/solarisstudio12.3//bin/CC ../a.cpp
bash-4.1$ cd ..
bash-4.1$ diff temp1/a.out temp2/a.out
Binary files temp1/a.out and temp2/a.out differ
bash-4.1$ cmp temp1/a.out temp2/a.out
temp1/a.out temp2/a.out differ: char 5968, line 24
bash-4.1$ od -x temp1/a.out > temp1/a.hex
bash-4.1$ od -x temp2/a.out > temp2/a.hex
bash-4.1$ diff -c temp1/a.hex temp2/a.hex 
... shows hex diff (two sets) ...

回答1:


The compiler is embedding information that is useful for debuggers. This information is by default in the dwarf format.

You can use the dwarfdump command to extract this information for the binary files. If you compare the output generated for each file, you'll see that the directory where the original binary was compiled is stored in a couple of location in the dwarf header, e.g.:

# diff */*.dwarf
9c9
<                     DW_AT_comp_dir              "/tmp/temp1/"
---
>                     DW_AT_comp_dir              "/tmp/temp2/"
29c29
<    2:  N_CMDLINE 0x0,0x0,0x0  "/tmp/temp1/; /opt/solarisstudio12.4/bin/CC  ../a.c"
---
>    2:  N_CMDLINE 0x0,0x0,0x0  "/tmp/temp2/; /opt/solarisstudio12.4/bin/CC  ../a.c"

Stripping the binary (strip a.out) should remove that header and make both compiled files identical.

Should you use an older release of the compiler or use the -xdebugformat=stabs compiler option, the debugging information would have been stored in stabs format instead of dwarf. In such case, the command to extract it is dumpstabs and would find similar differences related to the compilation directory, e.g.:

.stabs "/tmp/temp1/; /opt/solarisstudio12.4/bin/CC -xdebugformat=stabs  ../a.c",N_CMDLINE,0x0,0x0,0x0



回答2:


One information that I got on this is to use -zstrip-class=debug option, which removes this path info from object file. Checked that after using this option, there is no difference between the two object files created from two different folders. However, as this page (https://docs.oracle.com/cd/E26502_01/html/E29030/ld-1.html) shows, this option removes sections related to debug info from object file. It seems there is no simpler option which removes only the path info and keeps other debug info, when needed.



来源:https://stackoverflow.com/questions/36589690/solaris-studio-adding-current-directory-info-in-object-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!