c语言 - 修复"warning: output has a corrupt string table index"



我正试图编写一个可链接到文件的ELF,但objdump和ld总是给出以下警告:"输出具有损坏的字符串表索引";。objdump无法获得汇编的代码,并且在使用ld后,它不会执行任何内容。我尝试过打乱大小、偏移量和字符串索引值,但都没有成功。我也尝试过寻找解决方案,但一无所获。

#include <iostream>
#include <elf.h>
int main() {
// ELF header
Elf64_Ehdr ELFheader;
ELFheader.e_ident[EI_MAG0] = ELFMAG0; // Magic shit
ELFheader.e_ident[EI_MAG1] = ELFMAG1;
ELFheader.e_ident[EI_MAG2] = ELFMAG2;
ELFheader.e_ident[EI_MAG3] = ELFMAG3;
ELFheader.e_ident[EI_CLASS] = ELFCLASS64; // 64 bit
ELFheader.e_ident[EI_DATA] = ELFDATA2LSB; // Little edain
ELFheader.e_ident[EI_VERSION] = EV_CURRENT; // Current ELF version
ELFheader.e_ident[EI_OSABI] = ELFOSABI_SYSV; // System V
ELFheader.e_ident[EI_ABIVERSION] = 0;

ELFheader.e_type = ET_REL;
ELFheader.e_machine = EM_X86_64; // AMD x86
ELFheader.e_version = EV_CURRENT;
ELFheader.e_entry = 0;
ELFheader.e_phoff = 0;
ELFheader.e_shoff = 64; // Sections start 64 bytes in the file
ELFheader.e_flags = 0;
ELFheader.e_ehsize = 64; // Header size
ELFheader.e_phentsize = 0; // Program header size
ELFheader.e_phnum = 0; // Number of program headers
ELFheader.e_shentsize = 64;
ELFheader.e_shnum = 1; // 2 Section headers
ELFheader.e_shstrndx = 1;
// Section header
Elf64_Shdr TextSection;
TextSection.sh_name = DT_TEXTREL; // .text
TextSection.sh_type = SHT_PROGBITS;
TextSection.sh_flags = SHF_EXECINSTR;
TextSection.sh_addr = 0;
TextSection.sh_offset = 128;
TextSection.sh_size = 13;
TextSection.sh_link = 0;
TextSection.sh_info = 0;
TextSection.sh_addralign = 16;
TextSection.sh_entsize = 0; // Section header index
unsigned char code[13] = "xb8x3cx00x00x00xbfx02x00x00x00x0fx05"; // Code, exits with a syscall

{
FILE *fout = fopen("output", "w");
fwrite(&ELFheader, sizeof(ELFheader), 1, fout);
fclose(fout);
}
{
FILE *fout = fopen("output", "a");
fwrite(&TextSection, sizeof(TextSection), 1, fout);
fclose(fout);
}
{
FILE *fout = fopen("output", "a");
fwrite(&code, 13, 1, fout);
fclose(fout);
}
return 0;
}

你会怎么解决这个问题?

DT_TEXTREL绝对不是节名或其索引。您需要将节名称实际存储在文件中,并在sh_name中引用它。

最新更新