如何扩展ELF二进制文件
我正在编写一个小型仪器工具.我必须在二进制文件中插入检测例程.一个好的方法应该是将那些例程插入一个单独的代码段和一个单独的数据段中,您能解释一下如何做到这一点吗?此外,如何修改原始文件中代码段的大小?
I am writing a small instrumentation tool. I must insert the instrumentation routine within the binary file. A good approach should be to insert those routines in a separate code segment and a separate data segment, could you explain how to accomplish this? Furthemore how can I modify the size of the code segment in the original file?
最佳,
我必须在二进制文件中插入检测例程.一个好的方法应该是将这些例程插入单独的代码段和单独的数据段中
I must insert the instrumentation routine within the binary file. A good approach should be to insert those routines in a separate code segment and a separate data segment
什么是binary file
?对可重定位(ET_REL
)对象文件执行此操作与对完全链接的可执行文件(ET_EXEC
)或共享库(ET_DYN
)执行此操作之间有一个 big 区别. /p>
What is a binary file
? There is a big difference between doing this for a relocable (ET_REL
) object file, vs. doing this for a fully linked executable (ET_EXEC
)or shared library (ET_DYN
).
您能解释一下如何实现吗?
could you explain how to accomplish this?
对于ET_REL
,它应该相当简单:读取文件头,该文件头指向节头,告诉您.data
和.text
节的位置.然后,您编写一个新文件,扩展所需的部分,复制其他所有内容,并调整部分标题以反映新的部分偏移量和大小.
For an ET_REL
, it should be fairly straight-forward: you read the file header, which points to section headers, which tells you where .data
and .text
sections are. You then write a new file, extending the sections you want, copying everything else, and adjusting the section headers to reflect new section offsets and sizes.
对于ET_DYN
或ET_EXEC
,问题很可能是太难了:您需要调整重定位表,哈希表,程序头;保持所有结构自洽并正确对齐.
For an ET_DYN
or ET_EXEC
, the problem is very likely too hard: you'll need to adjust relocation tables, hash tables, program headers; keeping all the structures self-consistent and properly aligned.