且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Solaris Studio在目标文件中添加当前目录信息

更新时间:2023-11-30 12:41:52

编译器嵌入对调试器有用的信息。默认情况下,此信息的矮人格式。



您可以使用 dwarfdump 命令提取二进制文件的此信息。如果你比较为每个文件生成的输出,你会看到原始二进制被编译的目录存储在dwarf头文件中的几个位置,例如:

 #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 ../ac

清除二进制文件( strip a.out )应删除该标题,并使两个编译文件相同。



如果您使用较早版本的编译器或使用 -xdebugformat = stabs 编译器选项,调试信息将存储在 stabs 格式而不是dwarf。在这种情况下,解压缩的命令是 dumpstabs 并且会发现与编译目录相似的差异,例如:

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


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) ...

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