Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I access a symbol from the linker script in my Ada code?

I am building my Ada/SPARK project using GNAT and I am using a linker script. Here is an excerpt:

SECTIONS
{
    .code :
    {
        . = ALIGN(0x4);
        *(.text.section1)
        _end_of_section1 = .;
        *(.text.section2)
        ...
    }
}

The symbol _end_of_section1 is the address between the two sections. I'd like to be able to access this in my Ada code. I know it's possible in C using extern char _end_of_section1[];. Is it possible to do something like this in Ada? If not, is there some other way to get this address in the code?

like image 213
Nola Avatar asked Sep 12 '25 19:09

Nola


1 Answers

You can import a linker symbol by using the Importand Link_Name aspects (see also RM B.1):

main.adb (updated on 25-jan)

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;

output

$ ./obj/main 
Address : 0000000000403300
Value   : 16#F3#

output (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
  ...
like image 179
DeeDee Avatar answered Sep 17 '25 20:09

DeeDee