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.

Source: Internet
Author: User
Tags fread
# 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.