将固定大小内存边界上的本机代码与 GCC/G++/AS 对齐?

发布于 2024-07-29 03:07:33 字数 264 浏览 8 评论 0原文

我有一个 C 函数,其中包含将实现字节码解释器的字节码的所有代码。

我想知道是否有一种方法可以将内存中已编译代码的段对齐固定大小的边界,以便我可以直接从字节码的值计算要跳转到的地址? 与数组的工作方式类似,但我不是从计算的地址读取,而是跳转到它。

我知道我必须将代码放在每个“字节码代码”段的末尾执行下一个跳转,并且我必须使边界大小至少与最大段的大小一样大。

如果这甚至可能,我将如何告诉编译器/汇编器(gcc / g++ / as)以上述方式对齐?

I have a C function that contains all the code that will implement the bytecodes of a bytecode interpreter.

I'm wondering if there is a way to align segments of the compiled code in memory on fixed size boundaries so that I could directly calculate the address to jump to from the value of the bytecode? Sort of the same way an array works but instead of reading from the calculated address, I'm jumping to it.

I am aware that i will have to put the code to perform the next jump at the end of every "bytecode code" segment and that I will have to make the boundary size at least as big as the size of the largest segment.

If this is even possible, how would I tell the compiler/assembler (gcc / g++ / as) to align in said way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

数理化全能战士 2024-08-05 03:07:33

我意识到这并不完全是您所要求的,但这是使用 GCC 实现字节码解释器的标准方法。

GCC 的“计算转到”或“标签作为值”功能允许您将标签放入数组中并有效地跳转到不同的字节码指令。 请参阅使用 gcc 计算 goto 的快速解释器。 另请查看相关的 Stack Overflow 问题:C/C++ goto关于标签作为值的 GCC 文档

执行此操作的代码如下所示:

void* jumptable[] = {&&label1, &&label2};

label:
  /* Code here... */

label2:
  /* Other code here... */

然后您可以使用下表跳转到不同的指令:

goto *jumptable[i];

I realize this is not exactly what you are asking for, but this is the standard way to implement byte code interpreters with GCC.

GCC's "computed goto" or "labels as values" feature allows you to put labels in an array and efficiently jump to different bytecode instructions. See Fast interpreter using gcc's computed goto. Also look at this related Stack Overflow question: C/C++ goto, and the GCC documentation on labels as values.

The code to do this would look something like this:

void* jumptable[] = {&&label1, &&label2};

label:
  /* Code here... */

label2:
  /* Other code here... */

You can then jump to different instructions using the table:

goto *jumptable[i];
断舍离 2024-08-05 03:07:33

这里有两个问题,但答案是相同的。 首先,您将(二进制)数据写入(二进制)文件。 其次,您将该(二进制)数据加载到内存中。 您可以控制它在磁盘上的位置,也可以控制它在内存中的位置。 您可以轻松计算出您要寻找的内容。

就我个人而言,在将数据加载到内存中时,我可能会使用数组,并且我会确保所有数据都从该数组中的有效索引开始。 数组是连续排列的并且相对容易使用。 Kernighan 和 Ritchie 的书《C 编程语言》提到了使用联合进行对齐的技术,但这并没有使指针算术变得更容易。

There are two issues here, but the answer is the same. First, you're writing (binary) data to a (binary) file. Second, you're loading that (binary) data into memory. You control where it goes on disk, and you control where it goes in memory. You can easily calculate what you're looking for.

Personally, I would probably use an array when loading data into memory, and I would make sure all data started at a valid index in that array. Arrays are laid out contiguously and are relatively easy to work with. Kernighan and Ritchie's book The C Programming Language mentions a technique of using unions for alignment, but that doesn't make pointer arithmetic any easier.

眼前雾蒙蒙 2024-08-05 03:07:33

如果您使用的是 Linux,请使用 posix_memalign()。 我确信Windows 也有类似的功能。

如果您想对齐自己的代码,请查看 gcc __attribute__ 语法。

ld -Ttext 选项也可能有帮助。

If you are using linux use posix_memalign(). I am sure there is a similar function for Windows.

If you want to align your own code have a look at the gcc __attribute__ syntax.

The ld -Ttext options may also be helpful.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文