我正在裸板运行时上运行,从地址零读取数据是我的软件中的有效用例。但是,运行时将地址 0x0 视为 null
,并在使用 -O2
编译时在以下代码中引发异常。使用 -O1
编译时,代码的行为符合预期:
declare
use System.Storage_Elements;
type Byte_Array is array (Natural range 0 .. 3) of Interfaces.Unsigned_8;
bytes : constant Byte_Array with Import, Convention => Ada, Address => To_Address(Integer_Address(16#00000000#));
begin
-- Copy bytes to some other byte array in memory:
other_bytes := bytes; -- Throws exception
end;
有办法解决这个问题吗?
<小时/>我的平台详细信息:
- Ravenscar 小型运行时移植到 Arm Cortex M1
- 在 Microsemi RTG4 FPGA 内实例化的软核处理器上运行
失败详细信息:
之前的代码在运行时在 s-memcop.adb
实现的 memcpy
函数中终止。下面复制了代码,并有一条注释报告了失败的行。我不确定抛出的实际异常。我所能看到的只是使用信息 s-memcop.adb:52
调用最后机会处理程序,该信息在下面进行了注释。
40 function memcpy
41 (Dest : Address; Src : Address; N : size_t) return Address
42 is
43 D : IA := To_IA (Dest);
44 S : IA := To_IA (Src);
45 C : size_t := N;
46
47 begin
48 -- Try to copy per word, if alignment constraints are respected
49
50 if ((D or S) and (Word'Alignment - 1)) = 0 then
51 while C >= Word_Unit loop
52 To_Word_Ptr (D).all := To_Word_Ptr (S).all; -- Last_Chance_Handler Called here :(
53 D := D + Word_Unit;
54 S := S + Word_Unit;
55 C := C - Word_Unit;
56 end loop;
57 end if;
58
59 -- Copy the remaining byte per byte
60
61 while C > 0 loop
62 To_Byte_Ptr (D).all := To_Byte_Ptr (S).all;
63 D := D + Byte_Unit;
64 S := S + Byte_Unit;
65 C := C - Byte_Unit;
66 end loop;
67
68 return Dest;
69 end memcpy;
有没有办法查看实际抛出的异常?
最佳答案
我与 AdaCore 取得了联系并收到了该问题的解决方案:
If you compile at -O2 or above, then you also need to pass to the compiler the switch -fno-delete-null-pointer-checks because -fdelete-null-pointer-checks is automatically enabled at -O2 for the ARM architecture.
根据docs -fdelete-null-pointer-checks
标志:
Assume that programs cannot safely dereference null pointers, and that no code or data element resides at address zero.
由于我的应用程序并非如此,并且我需要访问地址零处的数据,因此我需要通过向编译器传递 -fno-delete-null-pointer-checks
来禁用此开关选项。
注意:当我的代码和运行时的优化标志不一致时,我遇到了问题。在这种情况下,使用 -O2
和 -fno-delete-null-pointer-checks
选项编译我的代码和运行时允许我自由地读取/写入地址 0x0。
关于null - 从 Ada 中的地址 0x0 读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58631194/