# Include "elf. H"
# Include <stdio. h>
Int main (INT argc, char * argv [])
{
File * FP;
Int I = 0;
Int FD = 0;
Elf64_ehdr EHDR;
If (FP = fopen (argv [1], "R") = NULL)
{
Perror ("open ");
Return-1;
}
Else
{
Printf ("% s \ n", argv [1]);
FD = fread (& EHDR, 1, sizeof (elf64_ehdr), FP );
If (FD = sizeof (elf64_ehdr ))
{
If (EHDR. e_ident [ei_mag0]! = Elfmag0 |
EHDR. e_ident [ei_mag1]! = Elfmag1 |
EHDR. e_ident [ei_mag2]! = Elfmag2 |
EHDR. e_ident [ei_mag3]! = Elfmag3 ){
Perror ("the file is not a valid ELF File ");
Return-1;
}
Else {
Printf ("Number of table items in section header table % LD \ n", (long INT) EHDR. e_shnum); // Number of segment Headers
}
Elf64_shdr shdr [EHDR. e_shnum];
Fseek (FP, EHDR. e_shoff, 0 );
For (I = 0; I <EHDR. e_shnum; I ++)
{
Fread (& shdr [I], EHDR. e_shentsize, 1, FP );
}
Printf ("the address of the header string table: \ n ");
Fseek (FP, EHDR. e_shoff, 0 );
Fread (shdr, sizeof (elf64_shdr), EHDR. e_shnum, FP); // point to the header table
Long shstraddr = shdr [EHDR. e_shstrndx]. sh_offset;
Printf ("% P \ n", shdr [EHDR. e_shstrndx]. sh_offset); // address of the header string table
Fseek (FP, shstraddr, 0 );
Char Buf [1024] = {0 };
Fread (BUF, 1, sizeof (BUF), FP); // point to the header string table
Printf ("name string of each section: \ n ");
For (I = 0; I <EHDR. e_shnum; I ++)
Printf ("% s \ n", & Buf [shdr [I]. sh_name]);
}
Fclose (FP );
}
Return 0;
}
Check whether the opened file is in ELF format. If yes, the address of the header string table is output, and the names of each section are output in sequence.