Как получить Type:FUNC и его имя из файла ELF?Linux

Ответить
Anonymous
 Как получить Type:FUNC и его имя из файла ELF?

Сообщение Anonymous »

Код: Выделить всё

include 
#include 
#include 
#include 
#include 

int main(int argc, char *argv[]) {
if (argc != 2) {
fprintf(stderr, "Usage: %s \n", argv[0]);
return 1;
}
int fd = open(argv[1], O_RDONLY);
if (fd == -1) {
perror("Opening ELF file");
return 1;
}
Elf32_Ehdr ehdr;
if (read(fd, &ehdr, sizeof(ehdr)) != sizeof(ehdr)) {
perror("Reading ELF header");
close(fd);
return 1;
}
printf("e_type:%d\n", ehdr.e_type);
printf("e_machine:%d\n", ehdr.e_machine);
printf("e_section_off:%d\n", ehdr.e_shoff);
printf("e_section_num:%d\n", ehdr.e_shnum);
Elf32_Shdr symtab_hdr;
for (int i = 0; i < ehdr.e_shnum; i++) {
Elf32_Shdr shdr;
lseek(fd, ehdr.e_shoff + i * ehdr.e_shentsize, SEEK_SET); // seek offset
read(fd, &shdr, sizeof(shdr));
if (shdr.sh_type == SHT_SYMTAB) {
symtab_hdr = shdr;
printf("index:%d, symtab_offset:%x\n",i , symtab_hdr.sh_offset);
break;
}
}

Elf32_Shdr strtab_hdr;
for (int i = 0; i < ehdr.e_shnum;i++) {
Elf32_Shdr shdr;
lseek(fd, ehdr.e_shoff + i * ehdr.e_shentsize, SEEK_SET); // seek offset
read(fd, &shdr, sizeof(shdr));

if (shdr.sh_type == SHT_STRTAB) {
strtab_hdr = shdr;
printf("index:%d, strtab_offset:%x\n",i, strtab_hdr.sh_offset);
break;
}
}

close(fd);
return 0;
}
Я получаю таблицу символов, но когда я хочу получить таблицу строк, я получаю другой тип, также SHT_STRTAB.

Код: Выделить всё

e_type:3
e_machine:3
e_section_off:13780
e_section_num:29
index:26, symtab_offset:3034
index:6, strtab_offset:28c

Код: Выделить всё

Section Headers:
[Nr] Name              Type            Addr     Off    Size   ES Flg Lk Inf Al
[ 0]                   NULL            00000000 000000 000000 00      0   0  0
[ 1] .interp           PROGBITS        00000194 000194 000013 00   A  0   0  1
[ 2] .note.gnu.bu[...] NOTE            000001a8 0001a8 000024 00   A  0   0  4
[ 3] .note.ABI-tag     NOTE            000001cc 0001cc 000020 00   A  0   0  4
[ 4] .gnu.hash         GNU_HASH        000001ec 0001ec 000020 04   A  5   0  4
[ 5] .dynsym           DYNSYM          0000020c 00020c 000080 10   A  6   1  4
[ 6] .dynstr           STRTAB          0000028c 00028c 0000a6 00   A  0   0  1
[ 7] .gnu.version      VERSYM          00000332 000332 000010 02   A  5   0  2
[25] .comment          PROGBITS        00000000 003008 00002b 01  MS  0   0  1
[26] .symtab           SYMTAB          00000000 003034 000280 10     27  18  4
[27] .strtab           STRTAB          00000000 0032b4 000222 00      0   0  1
[28] .shstrtab         STRTAB          00000000 0034d6 0000fc 00      0   0  1
Итак, как точно найти .strtab и использовать .strtab и таблицы символов, чтобы найти FUNC и получить его «имя» (это смещение, затем получить настоящее имя в strtab) ?

Код: Выделить всё

Symbol table '.symtab' contains 40 entries:
Num:    Value  Size Type    Bind   Vis      Ndx Name
0: 00000000     0 NOTYPE  LOCAL  DEFAULT  UND
1: 00000000     0 FILE    LOCAL  DEFAULT  ABS Scrt1.o
2: 000001cc    32 OBJECT  LOCAL  DEFAULT    3 __abi_tag
3: 00000000     0 FILE    LOCAL  DEFAULT  ABS crtstuff.c
4: 000010b0     0 FUNC    LOCAL  DEFAULT   14 deregister_tm_clones
5: 000010f0     0 FUNC    LOCAL  DEFAULT   14 register_tm_clones
Я пытаюсь использовать .shstrtab, чтобы найти название заголовков разделов. Но это тоже STRTAB.

Подробнее здесь: https://stackoverflow.com/questions/785 ... a-elf-file
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «Linux»