这个问题是我之前提出的另一个问题的延伸。简而言之,这是我试图将两个完全链接的可执行文件合并成一个完全链接的可执行文件的尝试之一。与之前的问题不同之处在于,之前的问题涉及将对象文件合并到完全链接的可执行文件中,这更难,因为这意味着我需要手动处理重定位。
我有以下文件:
我的目标是将这两个可执行文件合并成一个最终的可执行文件,该文件与
从BFD库的角度来看,每个二进制文件都由一组节(section)组成。我面临的第一个问题是这些节具有冲突的加载地址(如果我要合并它们,这些节将重叠)。
为了解决这个问题,我通过程序分析
在我的系统上,生成的链接器命令如下:
(请注意,我使用
然后我编写了一些代码来生成一个新的可执行文件(从
在我的系统上,
我不熟悉ELF层,但我认为这可能是读取动态符号表的问题(或者说它不存在)。目前,除非必须,我正在尝试避免直接进入ELF层。是否有人能告诉我我的代码或概念上哪里出错了?
如果有帮助的话,我也可以发布链接器命令生成的代码或示例二进制文件的编译版本。
我意识到这是一个非常大的问题,因此,我希望能够适当奖励那些能够帮助我的人。如果有人能帮助我解决这个问题,我很乐意支付500+的奖金。
我有以下文件:
example-target.c
:#include <stdlib.h>
#include <stdio.h>
int main(void)
{
puts("1234");
return EXIT_SUCCESS;
}
example-embed.c
:
#include <stdlib.h>
#include <stdio.h>
/*
* Fake main. Never used, just there so we can perform a full link.
*/
int main(void)
{
return EXIT_SUCCESS;
}
void func1(void)
{
puts("asdf");
}
我的目标是将这两个可执行文件合并成一个最终的可执行文件,该文件与
example-target
相同,但还有另一个main
和func1
函数。从BFD库的角度来看,每个二进制文件都由一组节(section)组成。我面临的第一个问题是这些节具有冲突的加载地址(如果我要合并它们,这些节将重叠)。
为了解决这个问题,我通过程序分析
example-target
,获取其每个节的加载地址和大小列表。然后我对example-embed
进行了相同的操作,并使用这些信息动态生成了一个链接命令,以确保example-embed.c
的所有节被链接到不与example-target
中的任何节重叠的地址。因此,在此过程中实际上完全链接了两次example-embed
:一次确定有多少节以及它们的大小,再一次以确保与example-target
没有节冲突而链接。在我的系统上,生成的链接器命令如下:
-Wl,--section-start=.new.interp=0x1004238,--section-start=.new.note.ABI-tag=0x1004254,
--section-start=.new.note.gnu.build-id=0x1004274,--section-start=.new.gnu.hash=0x1004298,
--section-start=.new.dynsym=0x10042B8,--section-start=.new.dynstr=0x1004318,
--section-start=.new.gnu.version=0x1004356,--section-start=.new.gnu.version_r=0x1004360,
--section-start=.new.rela.dyn=0x1004380,--section-start=.new.rela.plt=0x1004398,
--section-start=.new.init=0x10043C8,--section-start=.new.plt=0x10043E0,
--section-start=.new.text=0x1004410,--section-start=.new.fini=0x10045E8,
--section-start=.new.rodata=0x10045F8,--section-start=.new.eh_frame_hdr=0x1004604,
--section-start=.new.eh_frame=0x1004638,--section-start=.new.ctors=0x1204E28,
--section-start=.new.dtors=0x1204E38,--section-start=.new.jcr=0x1204E48,
--section-start=.new.dynamic=0x1204E50,--section-start=.new.got=0x1204FE0,
--section-start=.new.got.plt=0x1204FE8,--section-start=.new.data=0x1205010,
--section-start=.new.bss=0x1205020,--section-start=.new.comment=0xC04000
(请注意,我使用
objcopy --prefix-sections=.new example-embedobj
为节名称加上了.new
前缀,以避免节名称冲突。)然后我编写了一些代码来生成一个新的可执行文件(从
objcopy
和Security Warrior
书籍中借鉴了一些代码)。新的可执行文件应该具有:
example-target
的所有节和example-embed
的所有节- 一个符号表,其中包含
example-target
的所有符号和example-embed
的所有符号
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <bfd.h>
#include <libiberty.h>
struct COPYSECTION_DATA {
bfd * obfd;
asymbol ** syms;
int symsize;
int symcount;
};
void copy_section(bfd * ibfd, asection * section, PTR data)
{
struct COPYSECTION_DATA * csd = data;
bfd * obfd = csd->obfd;
asection * s;
long size, count, sz_reloc;
if((bfd_get_section_flags(ibfd, section) & SEC_GROUP) != 0) {
return;
}
/* get output section from input section struct */
s = section->output_section;
/* get sizes for copy */
size = bfd_get_section_size(section);
sz_reloc = bfd_get_reloc_upper_bound(ibfd, section);
if(!sz_reloc) {
/* no relocations */
bfd_set_reloc(obfd, s, NULL, 0);
} else if(sz_reloc > 0) {
arelent ** buf;
/* build relocations */
buf = xmalloc(sz_reloc);
count = bfd_canonicalize_reloc(ibfd, section, buf, csd->syms);
/* set relocations for the output section */
bfd_set_reloc(obfd, s, count ? buf : NULL, count);
free(buf);
}
/* get input section contents, set output section contents */
if(section->flags & SEC_HAS_CONTENTS) {
bfd_byte * memhunk = NULL;
bfd_get_full_section_contents(ibfd, section, &memhunk);
bfd_set_section_contents(obfd, s, memhunk, 0, size);
free(memhunk);
}
}
void define_section(bfd * ibfd, asection * section, PTR data)
{
bfd * obfd = data;
asection * s = bfd_make_section_anyway_with_flags(obfd,
section->name, bfd_get_section_flags(ibfd, section));
/* set size to same as ibfd section */
bfd_set_section_size(obfd, s, bfd_section_size(ibfd, section));
/* set vma */
bfd_set_section_vma(obfd, s, bfd_section_vma(ibfd, section));
/* set load address */
s->lma = section->lma;
/* set alignment -- the power 2 will be raised to */
bfd_set_section_alignment(obfd, s,
bfd_section_alignment(ibfd, section));
s->alignment_power = section->alignment_power;
/* link the output section to the input section */
section->output_section = s;
section->output_offset = 0;
/* copy merge entity size */
s->entsize = section->entsize;
/* copy private BFD data from ibfd section to obfd section */
bfd_copy_private_section_data(ibfd, section, obfd, s);
}
void merge_symtable(bfd * ibfd, bfd * embedbfd, bfd * obfd,
struct COPYSECTION_DATA * csd)
{
/* set obfd */
csd->obfd = obfd;
/* get required size for both symbol tables and allocate memory */
csd->symsize = bfd_get_symtab_upper_bound(ibfd) /********+
bfd_get_symtab_upper_bound(embedbfd) */;
csd->syms = xmalloc(csd->symsize);
csd->symcount = bfd_canonicalize_symtab (ibfd, csd->syms);
/******** csd->symcount += bfd_canonicalize_symtab (embedbfd,
csd->syms + csd->symcount); */
/* copy merged symbol table to obfd */
bfd_set_symtab(obfd, csd->syms, csd->symcount);
}
bool merge_object(bfd * ibfd, bfd * embedbfd, bfd * obfd)
{
struct COPYSECTION_DATA csd = {0};
if(!ibfd || !embedbfd || !obfd) {
return FALSE;
}
/* set output parameters to ibfd settings */
bfd_set_format(obfd, bfd_get_format(ibfd));
bfd_set_arch_mach(obfd, bfd_get_arch(ibfd), bfd_get_mach(ibfd));
bfd_set_file_flags(obfd, bfd_get_file_flags(ibfd) &
bfd_applicable_file_flags(obfd));
/* set the entry point of obfd */
bfd_set_start_address(obfd, bfd_get_start_address(ibfd));
/* define sections for output file */
bfd_map_over_sections(ibfd, define_section, obfd);
/******** bfd_map_over_sections(embedbfd, define_section, obfd); */
/* merge private data into obfd */
bfd_merge_private_bfd_data(ibfd, obfd);
/******** bfd_merge_private_bfd_data(embedbfd, obfd); */
merge_symtable(ibfd, embedbfd, obfd, &csd);
bfd_map_over_sections(ibfd, copy_section, &csd);
/******** bfd_map_over_sections(embedbfd, copy_section, &csd); */
free(csd.syms);
return TRUE;
}
int main(int argc, char **argv)
{
bfd * ibfd;
bfd * embedbfd;
bfd * obfd;
if(argc != 4) {
perror("Usage: infile embedfile outfile\n");
xexit(-1);
}
bfd_init();
ibfd = bfd_openr(argv[1], NULL);
embedbfd = bfd_openr(argv[2], NULL);
if(ibfd == NULL || embedbfd == NULL) {
perror("asdfasdf");
xexit(-1);
}
if(!bfd_check_format(ibfd, bfd_object) ||
!bfd_check_format(embedbfd, bfd_object)) {
perror("File format error");
xexit(-1);
}
obfd = bfd_openw(argv[3], NULL);
bfd_set_format(obfd, bfd_object);
if(!(merge_object(ibfd, embedbfd, obfd))) {
perror("Error merging input/obj");
xexit(-1);
}
bfd_close(ibfd);
bfd_close(embedbfd);
bfd_close(obfd);
return EXIT_SUCCESS;
}
简单概括一下这段代码的作用,它需要2个输入文件(ibfd
和embedbfd
)来生成一个输出文件(obfd
)。
- 从
ibfd
中复制格式/架构/机器/文件标志和起始地址到obfd
- 将
ibfd
和embedbfd
的部分定义到obfd
。由于BFD要求在开始填充之前创建所有部分,因此填充部分是分开进行的。 - 合并两个输入BFD的私有数据到输出BFD。由于BFD是许多文件格式上面的通用抽象,因此它不一定能够全面地封装底层文件格式所需的所有内容。
- 创建组合符号表,由
ibfd
和embedbfd
的符号表组成,并将其设置为obfd
的符号表。该符号表被保存以便稍后用于构建重定位信息。 - 将
ibfd
的部分复制到obfd
。除了复制部分内容外,此步骤还处理构建和设置重定位表。
在上面的代码中,一些行使用/******** */
进行了注释。这些行处理example-embed
的合并。如果它们被注释掉,那么obfd
就只是ibfd
的副本。我已经测试过了,它可以正常工作。但是,一旦我取消这些行的注释,问题就开始出现。
使用取消注释的完整合并版本仍然会生成一个输出文件。可以使用objdump
检查此输出文件,并发现它具有两个输入的所有部分、代码和符号表。但是,objdump
会抱怨:
BFD: BFD (GNU Binutils for Ubuntu) 2.21.53.20110810 assertion fail ../../bfd/elf.c:1708
BFD: BFD (GNU Binutils for Ubuntu) 2.21.53.20110810 assertion fail ../../bfd/elf.c:1708
在我的系统上,
elf.c
文件的第1708
行是:BFD_ASSERT (elf_dynsymtab (abfd) == 0);
elf_dynsymtab
是 elf-bfd.h
中的宏,用于:
#define elf_dynsymtab(bfd) (elf_tdata(bfd) -> dynsymtab_section)
我不熟悉ELF层,但我认为这可能是读取动态符号表的问题(或者说它不存在)。目前,除非必须,我正在尝试避免直接进入ELF层。是否有人能告诉我我的代码或概念上哪里出错了?
如果有帮助的话,我也可以发布链接器命令生成的代码或示例二进制文件的编译版本。
我意识到这是一个非常大的问题,因此,我希望能够适当奖励那些能够帮助我的人。如果有人能帮助我解决这个问题,我很乐意支付500+的奖金。
example-embed
的作用),然后静态重定向新二进制代码的代码,以将原始代码与注入代码链接起来(我已经编写了反汇编/CFG分析引擎,并且我也可以编辑任意指令,因此这种注入是谜题的最后一块)。对于我需要关心的用例,可以假设我们可以访问用户定义例程的源代码,但无法访问目标文件的源代码。 - Mike Kwan.dynsymtab
节?(readelf -WS exename
) - Dan Fego