【发布时间】: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