comp.lang.ada
 help / color / mirror / Atom feed
* Linker_Alias
@ 2018-01-03 18:39 Jere
  2018-01-03 21:59 ` Linker_Alias Simon Wright
  0 siblings, 1 reply; 7+ messages in thread
From: Jere @ 2018-01-03 18:39 UTC (permalink / raw)


I know this is more of a GNAT question than an Ada question
but was hoping someone else had some insight to this pragma.

I am trying to use pragma Linker_Alias to set up a default
operation for an operation defined with pragma Weak_External.  


code is as follows:
************************************************************

package Operations is
   
   -- Will be called by the client
   procedure Target_Op
      with
         Import,
         Convention    => C,
         External_Name => "target",
         Link_Name     => "target";
   
   pragma Weak_External(Target_Op);
   pragma Linker_Alias
      (Entity => Target_Op,
       Target => "default_target");
   
private
   
   -- default operation if none supplied
   procedure Default
      with
         Export,
         Convention    => C,
         External_Name => "default_target",
         Link_Name     => "default_target";
   
end Operations;

with Ada.Text_IO; use Ada.Text_IO;

package body Operations is
   
   procedure Default is
   begin
      Put_Line("Default Operation");
   end Default;
 
end Operations;

with Ada.Text_IO; use Ada.Text_IO;
with Operations;

procedure Main is
begin
   Put_Line("Hello");
   Operations.Target_Op;
end Main;

************************************************************

It compiles fine but generates a runtime error that indicates
it cannot find any valid symbols.  The output is:

Hello

raised PROGRAM_ERROR : EXCEPTION_ACCESS_VIOLATION
[2018-01-03 13:25:34] process exited with status 1, elapsed time: 00.22s



The intent is that GNAT links in a default operation unless
the client exports a procedure with the intended link name.

The use case is for ZFP runtimes where I don't have access
to protected procedures.  I want to supply a dummy ISR
unless the client decides to override it.  

I know GCC itself supports this (I tested it in another
language just to make sure), but I cannot figure out how
to get GNAT to support it.

Am I setting up the pragma's wrong?  I've tried a few different
combinations, but with no luck.

I'm on Windows 10 using GNAT GPL 2017 (same problem on 2015 and 2016).


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Linker_Alias
  2018-01-03 18:39 Linker_Alias Jere
@ 2018-01-03 21:59 ` Simon Wright
  2018-01-04 21:09   ` Linker_Alias jhb.games
  0 siblings, 1 reply; 7+ messages in thread
From: Simon Wright @ 2018-01-03 21:59 UTC (permalink / raw)


Jere <jhb.chat@gmail.com> writes:

> I know this is more of a GNAT question than an Ada question
> but was hoping someone else had some insight to this pragma.
>
> I am trying to use pragma Linker_Alias to set up a default
> operation for an operation defined with pragma Weak_External.  
[...]
> The intent is that GNAT links in a default operation unless
> the client exports a procedure with the intended link name.
>
> The use case is for ZFP runtimes where I don't have access
> to protected procedures.  I want to supply a dummy ISR
> unless the client decides to override it.  
>
> I know GCC itself supports this (I tested it in another
> language just to make sure), but I cannot figure out how
> to get GNAT to support it.
>
> Am I setting up the pragma's wrong?  I've tried a few different
> combinations, but with no luck.
>
> I'm on Windows 10 using GNAT GPL 2017 (same problem on 2015 and 2016).

On macOS with GCC8 it failed to link (symbol "target" missing).

I never came across pragma Linker_Alias (and the description isn't easy
to get my head round). I had a similar problem in Cortex GNAT RTS: the
default was

   procedure HardFault_Handler
   with Export, Convention => Ada, External_Name => "HardFault_Handler";
   pragma Weak_External (HardFault_Handler);
   procedure HardFault_Handler is
   begin
      loop
         null;
      end loop;
   end HardFault_Handler;

and then, if required, override by providing a package containing

   procedure Handler
   with
     Export,
     Convention => Asm,
     External_Name => "HardFault_Handler";
   pragma Machine_Attribute (Handler, "naked");

whose body works out which stack is in use and sets up for debugging.

One thing that confused me for a while was that you have to ensure that
the overriding package actually gets linked; my comment says

   --  Normally, we'd mark this unit as preelaborable, but that would
   --  mean that the binder-generated code wouldn't reference it, so
   --  it wouldn't get referenced at all (because the weak symbol in
   --  startup.o would satisfy the link).

https://github.com/simonjwright/cortex-gnat-rts/blob/master/common/hardfault_handling.ads
(and .adb).


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Linker_Alias
  2018-01-03 21:59 ` Linker_Alias Simon Wright
@ 2018-01-04 21:09   ` jhb.games
  2018-01-04 22:56     ` Linker_Alias Simon Wright
  0 siblings, 1 reply; 7+ messages in thread
From: jhb.games @ 2018-01-04 21:09 UTC (permalink / raw)


On Wednesday, January 3, 2018 at 4:59:11 PM UTC-5, Simon Wright wrote:
> Jere writes:
> 
> > I know this is more of a GNAT question than an Ada question
> > but was hoping someone else had some insight to this pragma.
> >
> > I am trying to use pragma Linker_Alias to set up a default
> > operation for an operation defined with pragma Weak_External.  
> [...]
> > The intent is that GNAT links in a default operation unless
> > the client exports a procedure with the intended link name.
> >
> > The use case is for ZFP runtimes where I don't have access
> > to protected procedures.  I want to supply a dummy ISR
> > unless the client decides to override it.  
> >
> > I know GCC itself supports this (I tested it in another
> > language just to make sure), but I cannot figure out how
> > to get GNAT to support it.
> >
> > Am I setting up the pragma's wrong?  I've tried a few different
> > combinations, but with no luck.
> >
> > I'm on Windows 10 using GNAT GPL 2017 (same problem on 2015 and 2016).
> 
> On macOS with GCC8 it failed to link (symbol "target" missing).
> 
> I never came across pragma Linker_Alias (and the description isn't easy
> to get my head round). I had a similar problem in Cortex GNAT RTS: the
> default was
> 
>    procedure HardFault_Handler
>    with Export, Convention => Ada, External_Name => "HardFault_Handler";
>    pragma Weak_External (HardFault_Handler);
>    procedure HardFault_Handler is
>    begin
>       loop
>          null;
>       end loop;
>    end HardFault_Handler;
> 
> and then, if required, override by providing a package containing
> 
>    procedure Handler
>    with
>      Export,
>      Convention => Asm,
>      External_Name => "HardFault_Handler";
>    pragma Machine_Attribute (Handler, "naked");
> 
> whose body works out which stack is in use and sets up for debugging.


Thanks!

I tried that but ran into the same problem.  I added:

   procedure Target_Op_2
      with
         Convention => Ada,
         Export,
         External_Name => "t2",
         Link_Name => "t2";
   pragma Weak_External(Target_Op_2);

to Operations.ads, and

   procedure Target_Op_2 is
   begin
      Put_Line("t2");
   end Target_Op_2;

to Operations.adb but still got the same exception when calling
from the entry procedure:

with Ada.Text_IO; use Ada.Text_IO;
with Operations;

procedure Main is
begin
   Put_Line("Hello");
   Operations.Target_Op_2;
end Main;  

********************
Hello

raised PROGRAM_ERROR : EXCEPTION_ACCESS_VIOLATION
********************

So it looks like it still isn't defaulting the procedure.

Note
that I am testing this in the standard windows GNAT GPL
version as opposed to the cross compiler.  I'll get to
that, but I had to redo my environment for it.  I just
wanted to test out the functionality.

Any thoughts?

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Linker_Alias
  2018-01-04 21:09   ` Linker_Alias jhb.games
@ 2018-01-04 22:56     ` Simon Wright
  2018-01-06 14:46       ` Linker_Alias Jere
  0 siblings, 1 reply; 7+ messages in thread
From: Simon Wright @ 2018-01-04 22:56 UTC (permalink / raw)


jhb.games@gmail.com writes:

> So it looks like it still isn't defaulting the procedure.

All I can say is, works for me (macOS, GCC 7.1.0, GNAT GPL 2017).

Have you done a clean rebuild?


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Linker_Alias
  2018-01-04 22:56     ` Linker_Alias Simon Wright
@ 2018-01-06 14:46       ` Jere
  2018-01-06 16:58         ` Linker_Alias Simon Wright
  0 siblings, 1 reply; 7+ messages in thread
From: Jere @ 2018-01-06 14:46 UTC (permalink / raw)


On Thursday, January 4, 2018 at 5:56:04 PM UTC-5, Simon Wright wrote:
> Jere writes:
> 
> > So it looks like it still isn't defaulting the procedure.
> 
> All I can say is, works for me (macOS, GCC 7.1.0, GNAT GPL 2017).
> 
> Have you done a clean rebuild?

I did try a clean and rebuild.  I even started a fresh project.  
Here are my files:

default.gpr
***************************************
project Default is

   for Source_Dirs use ("src/**");
   for Object_Dir use "obj";
   for Exec_Dir use "bin";
   for Main use ("main.adb");
   for Languages use ("Ada");

   package Ide is
      for Documentation_Dir use "doc";
   end Ide;

   package Pretty_Printer is
      for Default_Switches ("ada") use ("-cl3");
   end Pretty_Printer;

   package Compiler is
      for Default_Switches ("ada") use ("-gnatn", "-gnato", "-gnatE", "-fstack-check", "-gnat12");
   end Compiler;

end Default;
***************************************

src/main.adb
***************************************
with Ada.Text_IO; use Ada.Text_IO;
with Operations;

procedure Main is
begin
   Put_Line("Hello");
   Operations.Target_Op;
end Main;
***************************************

src/operations.ads
***************************************
package Operations is

   procedure Target_Op
      with
         Convention    => Ada,
         Export,
         External_Name => "target_op",
         Link_Name     => "target_op";
   pragma Weak_External(Target_Op);

end Operations;
***************************************

src/operations.adb
***************************************
with Ada.Text_IO; use Ada.Text_IO;

package body Operations is

   procedure Target_Op is
   begin
      Put_Line("Calling Target Op Default");
   end Target_Op;

end Operations;
***************************************

I tried to mimic your example and made sure Operations was
with'ed and called in the main subprogram.  I would have
included a zip file, but don't see that option in Google
Groups.

Does mine run exception free for you on Mac (If you don't mind
checking of course)?

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Linker_Alias
  2018-01-06 14:46       ` Linker_Alias Jere
@ 2018-01-06 16:58         ` Simon Wright
  2018-01-06 20:16           ` Linker_Alias Jere
  0 siblings, 1 reply; 7+ messages in thread
From: Simon Wright @ 2018-01-06 16:58 UTC (permalink / raw)


Jere <jhb.chat@gmail.com> writes:

> Does mine run exception free for you on Mac (If you don't mind
> checking of course)?

Yes, it does.

But I tried on Windows 7 running GNAT GPL 2017 and got the same result
as you. Below is a screen grab of the debug where the next instruction
is the offending one; looks to me as though the linker doesn't know how
to deal with the weak symbol???

Not much help, I know.

https://www.dropbox.com/s/mxvvop99yd82ccy/Screen%20grab%20for%20Jere.jpg?dl=0

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Linker_Alias
  2018-01-06 16:58         ` Linker_Alias Simon Wright
@ 2018-01-06 20:16           ` Jere
  0 siblings, 0 replies; 7+ messages in thread
From: Jere @ 2018-01-06 20:16 UTC (permalink / raw)


On Saturday, January 6, 2018 at 11:58:30 AM UTC-5, Simon Wright wrote:
> Jere writes:
> 
> > Does mine run exception free for you on Mac (If you don't mind
> > checking of course)?
> 
> Yes, it does.
> 
> But I tried on Windows 7 running GNAT GPL 2017 and got the same result
> as you. Below is a screen grab of the debug where the next instruction
> is the offending one; looks to me as though the linker doesn't know how
> to deal with the weak symbol???
> 
> Not much help, I know.
> 
> https://www.dropbox.com/s/mxvvop99yd82ccy/Screen%20grab%20for%20Jere.jpg?dl=0

Thanks for checking it.  That at least confirms it for me.  I might try
and send a bug report to AdaCore.  Problem is I don't know if it is actually
a bug since this is all implementation pragmas.  And in the case of
pragma Linker_Alias, they say they don't have to support it.  Right now
my work around on windows is to use outside of Ada means or just accept
the exception when nothing is used to override it.  I can live with that.

Thanks again!


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2018-01-06 20:16 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-01-03 18:39 Linker_Alias Jere
2018-01-03 21:59 ` Linker_Alias Simon Wright
2018-01-04 21:09   ` Linker_Alias jhb.games
2018-01-04 22:56     ` Linker_Alias Simon Wright
2018-01-06 14:46       ` Linker_Alias Jere
2018-01-06 16:58         ` Linker_Alias Simon Wright
2018-01-06 20:16           ` Linker_Alias Jere

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox