Two copies of constant appearing in the compiled Elf

纵然是瞬间 提交于 2019-12-11 15:06:09

问题


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

cSomeConstant : CONSTANT LONG_FLOAT := 100.0;

In the Map file we have basically

.rodata 0x40010000 (0x8)file.o
.rodata 0x40010008 pfileconsts__csomeconstant

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

If any other package(file2.adb) also accesses the cSomeConstant, it is making another copy in the section

.rodata 0x40010010 (0x8)file2.o

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.


回答1:


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



来源:https://stackoverflow.com/questions/50407178/two-copies-of-constant-appearing-in-the-compiled-elf

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