Executing machine code in an array
This conversion is making a few assumption about how the compiler
represents procedure (or function) access types, and system address.
I suggest finding out from your vendor how these are implemented.
----------------------------------------------------------------------
--
-- Casting example to a special procedure pointer.
--
with Ada.Unchecked_Conversion;
with System;
procedure Call is
type Grunt_Call is access procedure (Arg : Integer);
type Byte is mod 2**8;
for Byte'Size use 8;
type Byte_Array is array (Positive range <>) of Byte;
pragma Pack (Byte_Array);
Sample_Code : Byte_Array (1 .. 4);
function "+" is
new Ada.Unchecked_Conversion (System.Address, Grunt_Call);
Perform_Grunt : Grunt_Call := "+"(Sample_Code(Sample_Code'First)'Address);
begin
-- This is an x86 return instruction.
Sample_Code (1) := 16#C3#;
-- Processors with separate instruction and data caches may have to
-- perform a system specific operation to make these caches coherent.
-- Now we can execute.
Perform_Grunt.all (10);
end Call;
Contributed by: David Brown
Contributed on: May 27, 1999
License: Public Domain
Back