Solaris Studio在目标文件中添加当前目录信息 [英] Solaris Studio adding current directory info in object file

查看:144
本文介绍了Solaris Studio在目标文件中添加当前目录信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在Solaris 11上使用Solaris Studio 12.x构建一个简单的hello world程序。如果我从两个不同的文件夹编译同一个文件,我得到的目标文件的差异,如diff命令或cmp命令。



获取两个目标文件(od -x)的十六进制转储,并进行比较,并使用可将十六进制转换为ASCII的在线工具,发现差异对象文件包含从中开始编译的当前目录。



问题是如何告诉编译器不要在目标文件中包含当前目录的信息。尝试了几个优化选项 - 对解决这个问题没有帮助。



这里是我使用的代码和命令:

  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
二进制文件temp1 / a.out和temp2 / a.out different
bash-4.1 $ cmp temp1 / a.out temp2 / a.out
temp1 / a.out temp2 / a.out different: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
...显示十六进制diff(两组)...


解决方案

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



您可以使用 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

这篇关于Solaris Studio在目标文件中添加当前目录信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆