简体   繁体   English

获取单个 C 函数的可执行二进制文件

[英]Get Executable Binary for a Single C Function

I have a simple C function, called add :我有一个简单的 C 函数,称为add

int add(int a, int b) {
  return a + b;
}

I need to get the optimized executable binary code for this given function without any of the other side effects of the binary.我需要为这个给定的函数获取优化的可执行二进制代码,而不需要二进制的任何其他副作用。 This would then be dynamically loaded and modified to include templates via a JIT system.然后将通过 JIT 系统动态加载和修改以包含模板。 I have experimented with trying to use objdump, but that doesn't really work for getting the actual binary as the object file is not the actual executable.我已经尝试过尝试使用 objdump,但这对于获取实际的二进制文件并不起作用,因为目标文件不是实际的可执行文件。

I need to link together the moving parts of functions to include their binary together at runtime and enabled PROT_EXEC to execute all of these partials together as a single function and call as a function pointer.我需要将函数的移动部分链接在一起,以便在运行时将它们的二进制文件包含在一起,并启用 PROT_EXEC 将所有这些部分作为单个函数一起执行并作为函数指针调用。 The reason I need this is because I have a byte instruction encoding type which I would like to turn into JIT functions on the fly (similar to JIT compiling RegExp into validator functions).我需要这个的原因是因为我有一个字节指令编码类型,我想即时将其转换为 JIT 函数(类似于将 RegExp 编译为验证器函数的 JIT)。 If there are any other viable solutions I should consider, then please describe them.如果我应该考虑任何其他可行的解决方案,请描述它们。

The main reason I want to use this approach is because I can see the godbolt shows the binary for functions as self-contained, so hopefully I can do something similar to include the binary in my own project.我想使用这种方法的主要原因是因为我可以看到 Godbolt 将函数的二进制文件显示为自包含的,所以希望我可以做一些类似的事情来将二进制文件包含在我自己的项目中。

Thank you for any help.感谢您的任何帮助。

Note: the C function is entirely self-contained without referencing other functions注意:C 函数完全自包含,不引用其他函数

The easiest way is using IDA .最简单的方法是使用IDA You can compile your code (preferably with symbols) into ELF file/ EXE file and open the generated file in IDA.您可以将您的代码(最好带有符号)编译成 ELF 文件/EXE 文件并在 IDA 中打开生成的文件。 There you can see your function in the function window and then move into the hex window to copy the hex dump of your function.在那里,您可以在函数窗口中看到您的函数,然后进入十六进制窗口以复制函数的十六进制转储。

Note: There are a lot of things that can go wrong here, for example, you must compile your code with the flag -PIC so it wouldn't use absolute addresses.注意:这里有很多可能出错的地方,例如,您必须使用标志-PIC编译代码,这样它就不会使用绝对地址。

It is also recommended to know at least basic assembly.还建议至少了解基本组装。

This link might be interesting: https://github.com/Neetx/Shellcode-Extractor这个链接可能很有趣: https ://github.com/Neetx/Shellcode-Extractor

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM