常量的两个副本出现在已编译的Elf中 [英] Two copies of constant appearing in the compiled Elf
问题描述
我们基本上使用sparc架构,我们的语言是Ada,我们使用gnat编译器来编译我们的代码。
我们观察到一些有趣的事情。
We are basically using sparc architecture and our language is Ada we are using gnat compiler to compile our code. We observed something funny.
我们代码中的某些常量有两个或多个副本。
Some of the constant in our code are having two or more copies.
file.adb:
With FileConsts; USE FileConsts
Procedure SomeProcedure is
A : LONG_FLOAT;
Begin
A := cSomeConstant;
End SomeProcedure;
FileConsts.ads
FileConsts.ads
cSomeConstant : CONSTANT LONG_FLOAT := 100.0;
在地图文件中,我们基本上有
In the Map file we have basically
.rodata 0x40010000 (0x8)file.o
.rodata 0x40010008 pfileconsts__csomeconstant
在程序集中它正在访问file.o的区域,即0x40010000,而不是0x40010000。在二进制文件中,0x40010000和0x40010008的值实际上是相同的,因此程序的行为符合预期。但是,为什么编译器会这样做
In the Assembly it is accessing the area of file.o, i.e 0x40010000 instead if 0x40010008. In the binary file the value at 0x40010000 and 0x40010008 is actually same, so program is behaving as expected. But why would the compiler do that
如果任何其他包( file2.adb
)也访问cSomeConstant,它将正在该节中制作另一个副本
If any other package(file2.adb
) also accesses the cSomeConstant, it is making another copy in the section
.rodata 0x40010010 (0x8)file2.o
二进制文件中的值再次与 cSomeConstant
Again the value in binary file is same as cSomeConstant
为什么编译器具有这种方式?如何抑制这种行为?
Why compiler is behaving this way? How to suppress this behavior ?
调试时确实令人困惑。
It really is confusing while debugging.
推荐答案
您应该记住,键入的常量在Ada中不是静态的。
You should remember that typed "constants" aren't static in Ada.
如果需要静态常量,请使用命名数字:
If you want a static constant, use a "named number":
Some_Constant : constant := 100.0;
(我不知道在这种情况下编译器将生成哪种代码。)
(I don't know which code the compiler will generate in this case.)
这篇关于常量的两个副本出现在已编译的Elf中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!