我正在使用libbfd为windows写出包含x86-64代码的coff对象文件的内容。写入符号、节和重定位可以正常工作,但生成的文件没有在coff标头中设置机器类型。我在文件的开头手动编写了0x8664来解决这个问题。
有没有办法使用bfd API来设置对象的机器类型?
这是我编写对象文件的代码:
bfd_init();
auto bfd = bfd_openw("test.obj", "pe-x86-64");
if (!bfd) {
bfd_perror("bfd_openw failed");
return -1;
}
if (!bfd_set_format(bfd, bfd_object)) {
bfd_perror("bfd_set_format failed");
return -1;
}
// now write some sections, symbols and relocations
bfd_close(bfd);
// TODO hack: overwrite first two bytes of file to make it a AMD64 coff file
std::fstream obj_file("test.obj", std::ios::binary | std::ios::in | std::ios::out);
obj_file.seekp(0, std::ios::beg);
obj_file.write("\x64\x86", 2);发布于 2021-04-27 06:41:18
答案很简单,但在文档中很难找到……打开bfd对象时,必须使用bfd_set_arch_info来设置架构。
bfd_init();
auto bfd = bfd_openw("test.obj", "pe-x86-64");
if (!bfd) {
bfd_perror("bfd_openw failed");
return -1;
}
if (!bfd_set_format(bfd, bfd_object)) {
bfd_perror("bfd_set_format failed");
return -1;
}
// now set the architecture:
bfd_set_arch_info(bfd, bfd_lookup_arch(bfd_arch_i386, bfd_mach_x86_64));https://stackoverflow.com/questions/66835829
复制相似问题