By Falcon of TinyLab.org
Dec 09, 2019
背景简介
有一天,某位同学在讨论群聊起来:除了直接把 C 语言程序编译成 ELF 运行以外,是否可以转成二进制,然后通过第三方程序加载到内存后再运行。
带着这样的问题,我们写了四篇文章,这是其一。
这一篇先介绍如何把 ELF 文件转为二进制文件,然后把二进制文件作为一个 Section 加入到另外一个程序,然后在那个程序中访问该 Section 并运行。
准备工作
先准备一个非常简单的 X86 汇编:
# hello.s
# as --32 -o hello.o hello.s
# ld -melf_i386 -o hello hello.o
# objcopy -O binary hello hello.bin
#
.text
.global _start
_start:
xorl %eax, %eax
movb $4, %al # eax = 4, sys_write(fd, addr, len)
xorl %ebx, %ebx
incl %ebx # ebx = 1, standard output
movl $.LC0, %ecx # ecx = $.LC0, the addr of string
xorl %edx, %edx
movb $13, %dl # edx = 13, the length of .string
int $0x80
xorl %eax, %eax
movl %eax, %ebx # ebx