【问题标题】:How to use "&" in CIL .data-declarations如何在 CIL .data-declarations 中使用“&”
【发布时间】:2020-12-22 20:45:10
【问题描述】:

我正在编写一个针对通用中间语言的编译器,并希望将.data-declarations 用于全局变量。我的意思是Spec 的第 II.16.3.1 节。

如何使用“标签地址”的描述?以下确实使用 .NET-Core 的 ilasm 组装:

.assembly extern mscorlib
{
    .ver 4:0:0:0
}
.assembly 'string'
{
}
.module 'string'

.data hello_world_data = { int8(72), int8(101), int8(108), int8(108), int8(111), int8(32), int8(87), int8(111), int8(114), int8(108), int8(100), int8(33), int8(0) }

.field static int8 hello_world at hello_world_data

.data addr_of_data = &(hello_world_data)

.field static int8* hello_world_ptr at addr_of_data

.method public static default int32 main () cil managed {
    .entrypoint

    ldc.i4.0
    ret
}

但是当我尝试执行上面的代码时,我收到以下错误消息(在 Linux 上使用 .NET-Core):

Unhandled exception. System.BadImageFormatException: Could not load file or assembly '/home/lou/uni/proj/stuff/tests/test-global-arrays/string.exe'. An attempt was made to load a program with an incorrect format.

File name: '/home/lou/uni/proj/stuff/tests/test-global-arrays/string.exe'


[1]    12019 abort (core dumped)  dotnet string.exe

有什么想法/帮助吗?

【问题讨论】:

  • 在 Windows 上使用 .NET Framework ilasm 很有趣,它可以正常工作
  • 我不仅在 .NET-Core 上进行了测试,还使用 ​​Mono(Linux 上也是如此)。它也不适用于 Mono。这就是为什么我猜我做错了什么。在 .NET-Core 上,我可以组装 il,但运行生成的 .exe-file 会导致 BadImageFormatException
  • 这些功能不经常使用,所以有些平台不支持它们。使用嵌入式资源可能会更好,尽管它更复杂。

标签: .net-core compiler-construction cil


【解决方案1】:

如果这对您很重要,我建议在 https://github.com/dotnet/runtime 存储库中提交问题。

我认为这在 Windows 之外不起作用的原因是因为这些指令生成带有重定位的数据(可执行文件中的位置,一旦将可执行文件加载到内存中,就需要使用真实地址进行修复)。由于 .NET 中的可执行文件格式基于 Windows PE 格式,因此 Windows 在加载可执行文件时会处理重定位。

在 Windows 之外,CoreCLR 附带了一个小型“Windows 加载器模拟器”,它加载 .NET 可执行文件的方式与 Windows 加载它们的方式相同。但它可能会错过对这些重定位的处理。否则,IL 格式不会生成重定位。

但是这些重定位在很多地方都会有问题(我完全希望像 IL Linker 这样的工具会错过它们并损坏它们,我假设 Mono 也没有处理这些,它们将会有问题用于 AOT 编译等)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-21
    • 1970-01-01
    • 2012-09-26
    • 1970-01-01
    • 1970-01-01
    • 2021-05-01
    相关资源
    最近更新 更多