如何使用NASM作为图书馆? [英] How can I use NASM as a library?
问题描述
我想在C ++项目中包含NASM本身(汇编器)。我可以将NASM编译为共享库吗?
I would like to include NASM itself (the assembler) in a C++ project. Can I compile NASM as a shared library? If not, is there another assembler that works as a C or C++ library?
我检查了 libyasm ,但无法理解我如何使用它来组合我的代码。
I checked libyasm but couldn't understand how I can use it to assemble my code.
推荐答案
Woah,当我离开时,这爆炸了。
Woah, this exploded when I was away.
我已经通过篡改YASM源代码解决了这个问题,完全忘了SO的问题,因为它收到绝对没有注意8个月前。以下是详细信息,其次是更好的建议。
I had solved this problem by tampering with the YASM source code, and totally forgot about the question in SO as it received absolutely no attention 8 months ago. Below are the details, followed by a better suggestion.
对于我考虑的项目,我需要使用YASM作为库,我很匆忙,因为我为一家公司这样做。当时没有好的图书馆我知道;并且我的结论是,习惯于LLVM框架是一个过度的任务(因为我想要的是汇编单一的x86 - x86_64指令并接收字节)。
For the project that I had in mind, I needed to use YASM as a library, and I was in a hurry because I was doing this for a company. Back then there were no good libraries that I was aware of; and I had concluded that getting used to the LLVM framework was an overkill for the task (because all I wanted was to assemble singular x86 - x86_64 instructions and receive the bytes).
因此我下载了YASM的源代码。
So I downloaded the source code for YASM.
在插入代码一段时间后,我注意到可执行文件接收输入和输出文件的文件路径;并传递这两个字符串。我想在内存中的char数组用于输入和输出;不是文件。所以我想,也许如果我可以找到所有传递的FILE指针,我可以将它们转换为字符指针,并更改每个文件读/写数组操作。
Upon meddling with the code for a while, I noticed that the executable receives the file paths for input and output files; and passes these two strings along. I wanted char arrays in memory for the input and output; not files. So I figured, maybe if I could find all FILE pointers that are passed around, I can convert them to char pointers, and change every file read/write to array operations.
原来是比它的声音更麻烦。显然,YASM不打开输入/输出文件一次,并使用相同的FILE指针;而是传递文件路径字符串的副本。我需要一个脚本,可以为我做所有必要的更改,这对我不利。
This turned out to be even more cumbersome than it sounds. Apparently YASM does not open input/output files once and uses the same FILE pointers; instead it passes around copies of the filepath strings. I needed a script that could make all the necessary changes for me, this wasn't good for me.
最终,我发现所有fopen / fclose调用程序中一个脚本,并替换为my_fopen / my_fclose。对于每个文件,我做了这些替换,我包括我的头文件中我实现这两个函数。
Eventually, I found all fopen/fclose calls in the program with a script, and replaced them with my_fopen/my_fclose. For each file that I made these replacements, I included my header file in which I implemented these two functions.
在这两个函数,我检查了传入字符串,比较它与fake_file。如果它们相等,我传递一个假的FILE指针指向内存的两个部分,从函数调用fmemopen和open_memstream获得。否则我只是简单地调用实际的fopen / fclose函数。换句话说,我将这两个调用(仅针对给定的文件名)重定向到内存文件。然后,我调用文件名参数设置为'fake_file'的库。
In both of these functions, I checked the incoming string, compared it with "fake_file". If they are equal, I passed a 'fake' FILE pointer pointing to two portions of memory, obtained from the function calls fmemopen and open_memstream. Otherwise I simply called the actual fopen/fclose functions. In other words, I redirected these two calls (only for a given filename) to a memory file. Then, I called the library with the filename parameter set to 'fake_file'.
因为我已经限制自己到Linux,这个方法对我有用。我还发现(使用Valgrind)在库版本有一个内存泄漏,所以我写了一个非常原始的垃圾收集器。基本上,我包装malloc等,以跟踪所有未释放的分配,并在每次执行后清理它们。
Since I have had limited myself to Linux at that point, this approach worked for me. I also found out (using Valgrind) that there was a memory leak in the library version, so I wrote a very primitive garbage collector for it. Basically I wrapped malloc's etc. to keep track of all allocations that are not freed, and clean them after each execution.
这种方法还允许我使用自动化这些更改脚本。
This approach also allowed me to automate these changes using a script. Unfortunately I did all these in a company so I cannot leak any actual code.
更好的建议:
截至5月31日, 2016;您可以使用Keystone引擎。它是基于LLVM,但它远远更进一步,更多的提供。拆卸引擎Capstone和这是一个近乎完美的装配和拆卸的夫妇。如果你需要这些组件,我建议这些,而不是我描述的黑客。这两种发动机目前正在开发中;
Better suggestion: As of May 31, 2016; you can use Keystone Engine instead. It is "based on LLVM, but it goes much further with a lot more to offer." The disassembly engine Capstone and this are a near perfect couple for assembly and disassembly. If you need either of these components, I suggest these instead of doing the hacks I described. Both of these engines are currently being developed; and even though Keystone has some small bugs, Capstone is very robust at the moment.
TL; DR:使用 keystone 。
这篇关于如何使用NASM作为图书馆?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!