【问题标题】:Why does sun C++ compiler change symbol names when compiling with debug infos?为什么 sun C++ 编译器在使用调试信息进行编译时会更改符号名称?
【发布时间】:2011-10-28 07:26:38
【问题描述】:

我有这个源文件:

// ConstPointer.cpp
const short * const const_short_p_const = 0;
const short * const_short_p = 0;

并在使用和不使用调试信息的情况下对其进行编译(SUN C++ Compiler 5.10):

# CC ConstPointer.cpp -c -o ConstPointer.o
# CC -g ConstPointer.cpp -c -o ConstPointer-debug.o

以下是目标文件的符号名称没有调试信息:

# nm -C ConstPointer.o


ConstPointer.o:

[Index]   Value      Size    Type  Bind  Other Shndx   Name

[2]     |         0|       0|SECT |LOCL |0    |10     |
[3]     |         0|       0|SECT |LOCL |0    |9      |
[4]     |         0|       0|OBJT |LOCL |0    |6      |Bbss.bss
[1]     |         0|       0|FILE |LOCL |0    |ABS    |ConstPointer.cpp
[5]     |         0|       0|OBJT |LOCL |0    |3      |Ddata.data
[6]     |         0|       0|OBJT |LOCL |0    |5      |Dpicdata.picdata
[7]     |         0|       0|OBJT |LOCL |0    |4      |Drodata.rodata
[9]     |         4|       4|OBJT |GLOB |0    |3      |const_short_p
[8]     |         0|       4|OBJT |LOCL |0    |3      |const_short_p_const

以下是目标文件的符号名称带有调试信息:

# nm -C ConstPointer-debug.o


ConstPointer-debug.o:

[Index]   Value      Size    Type  Bind  Other Shndx   Name

[4]     |         0|       0|SECT |LOCL |0    |9      |
[2]     |         0|       0|SECT |LOCL |0    |8      |
[3]     |         0|       0|SECT |LOCL |0    |10     |
[10]    |         0|       4|OBJT |GLOB |0    |3      |$XAHMCqApZlqO37H.const_short_p_const
[5]     |         0|       0|NOTY |LOCL |0    |6      |Bbss.bss
[1]     |         0|       0|FILE |LOCL |0    |ABS    |ConstPointer.cpp
[6]     |         0|       0|NOTY |LOCL |0    |3      |Ddata.data
[7]     |         0|       0|NOTY |LOCL |0    |5      |Dpicdata.picdata
[8]     |         0|       0|NOTY |LOCL |0    |4      |Drodata.rodata
[9]     |         4|       4|OBJT |GLOB |0    |3      |const_short_p

为什么变量const_short_p_const 有另一个符号名称? g++ 在使用调试信息进行编译时不会更改它。对我来说,它看起来像一个编译器错误。你怎么看?第二个const(常量指针)指向这个。

编辑 Drew Hall 的评论: 例如你有两个文件:

// ConstPointer.cpp
const short * const const_short_p_const = 0;

void foo();

int main(int argc, const char *argv[]) {
  foo();
  return 0;
}

// ConstPointer2.cpp
extern const short * const const_short_p_const;

void foo() {
  short x = *const_short_p_const;
}

编译没问题:

# CC ConstPointer2.cpp -g -c -o ConstPointer2.o
# CC ConstPointer.cpp -g -c -o ConstPointer.o      

但链接不起作用,因为符号不同! ConstPointer2.o 中的符号名称为const_short_p_const,而ConstPointer.o 中的符号名称为$XAHMCqApZlqO37H.const_short_p_const

# CC ConstPointer.o ConstPointer2.o -o ConstPointer
Undefined                       first referenced
 symbol                             in file
const_short_p_const                 ConstPointer2.o

【问题讨论】:

  • 这纯粹是一个实现细节——你为什么关心生成的符号名称?

标签: c++ compiler-construction solaris symbols sun


【解决方案1】:

也许这与全局 const 变量在 C++ 中隐含为 static 的事实有关?

【讨论】:

  • 当然可以。如果你看他发布的输出,没有调试信息,绑定是本地的;有了它,绑定是全局的。 (我怀疑这里的动机是,一旦链接,所有本地符号都会消失。)从根本上说,对于调试符号,Sun CC 或多或少地将static 视为在匿名命名空间中。
  • 是的,在 C++ 中,全局 const 变量是隐式静态的,除非您明确声明它们 extern。这允许编译和链接上面的例子与这两个文件!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-14
  • 2015-03-24
  • 1970-01-01
  • 1970-01-01
  • 2023-03-13
  • 1970-01-01
相关资源
最近更新 更多