我正在使用 GNAT 构建我的 Ada/SPARK 项目,并且正在使用链接器脚本。以下是摘录:
SECTIONS
{
.code :
{
. = ALIGN(0x4);
*(.text.section1)
_end_of_section1 = .;
*(.text.section2)
...
}
}
符号_end_of_section1是两个节之间的地址。我希望能够在我的 Ada 代码中访问它。我知道在 C 语言中使用
extern char _end_of_section1[];
是可能的。在 Ada 中可以做这样的事情吗?如果没有,是否有其他方法可以在代码中获取该地址?
您可以使用
Import
和 Link_Name
方面导入链接器符号(另请参阅 RM B.1):
main.adb(1 月 25 日更新)
with System.Storage_Elements;
with System.Address_Image;
with Ada.Text_IO; use Ada.Text_IO;
procedure Main is
package SSE renames System.Storage_Elements;
package Storage_Element_IO is
new Ada.Text_IO.Modular_IO (SSE.Storage_Element);
use Storage_Element_IO;
Start_Symbol : aliased SSE.Storage_Element
with Import, Link_Name => "_start";
Start_Symbol_Addr : constant System.Address := Start_Symbol'Address;
begin
Put ("Address : ");
Put (System.Address_Image (Start_Symbol_Addr));
New_Line;
Put ("Value : ");
Put (Start_Symbol, Base => 16);
New_Line;
end Main;
输出
$ ./obj/main
Address : 0000000000403300
Value : 16#F3#
输出(objdump)
$ objdump -d -M intel ./obj/main | grep -A5 "<_start>"
0000000000403300 <_start>:
403300: f3 0f 1e fa endbr64
403304: 31 ed xor ebp,ebp
403306: 49 89 d1 mov r9,rdx
403309: 5e pop rsi
40330a: 48 89 e2 mov rdx,rsp
...