David
David

Reputation: 99

How read ELF header in C?

How to open the file and copy the info into another file. I just need to copy the magic number and version. void read_header(FILE *file, File *file2);

Upvotes: 9

Views: 30385

Answers (4)

Cyclonecode
Cyclonecode

Reputation: 29991

Depending on the platform, a header file already defining this structure might be available. For instance you can try to include elf.h if you're running linux:

#include <elf.h>
#include <stdio.h>
#include <string.h>

#if defined(__LP64__)
#define ElfW(type) Elf64_ ## type
#else
#define ElfW(type) Elf32_ ## type
#endif

void read_elf_header(const char* elfFile) {
  // Either Elf64_Ehdr or Elf32_Ehdr depending on architecture.
  ElfW(Ehdr) header;

  FILE* file = fopen(elfFile, "rb");
  if(file) {
    // read the header
    fread(&header, sizeof(header), 1, file);

    // check so its really an elf file
    if (memcmp(header.e_ident, ELFMAG, SELFMAG) == 0) {
       // this is a valid elf file
    }

    // finally close the file
    fclose(file);
  }
}

If you system does not already include the elf.h you can check how the elf header is structured here. Then you can create a struct to hold your data and simply read it like this:

typedef struct {
  uint8     e_ident[16];         /* Magic number and other info */
  uint16    e_type;              /* Object file type */
  uint16    e_machine;           /* Architecture */
  uint32    e_version;           /* Object file version */
  uint64    e_entry;             /* Entry point virtual address */
  uint64    e_phoff;             /* Program header table file offset */
  uint64    e_shoff;             /* Section header table file offset */
  uint32    e_flags;             /* Processor-specific flags */
  uint16    e_ehsize;            /* ELF header size in bytes */
  uint16    e_phentsize;         /* Program header table entry size */
  uint16    e_phnum;             /* Program header table entry count */
  uint16    e_shentsize;         /* Section header table entry size */
  uint16    e_shnum;             /* Section header table entry count */
  uint16    e_shstrndx;          /* Section header string table index */
} Elf64Hdr;

void read_elf_header(const char* elfFile, const char* outputFile) {
  struct Elf64Hdr header;

  FILE* file = fopen(elfFile, "rb");
  if(file) {
    // read the header
    fread(&header, 1, sizeof(header), file);

    // check so its really an elf file
    if(header.e_type == 0x7f &&
       header.e_ident[1] == 'E' &&
       header.e_ident[2] == 'L' &&
       header.e_ident[3] == 'F') {

       // write the header to the output file
       FILE* fout = fopen(outputFile, "wb");
       if(fout) {
         fwrite(&header, 1, sizeof(header), fout);
         fclose(fout);
       }
     }

    // finally close the file
    fclose(file);
  }
}

Upvotes: 18

anshkun
anshkun

Reputation: 115

  1. Include elf.h
  2. Open and Stat elf file
  3. Map file
  4. Typecast map address to Elf64_Ehdr and read

Upvotes: 3

Kamajii
Kamajii

Reputation: 1878

If you want bare-metal parsing, follow Cyclone's proposition or, e.g. have a look at other people doing so, like Qt in http://code.qt.io/cgit/qt/qtbase.git/tree/src/corelib/plugin/qelfparser_p.cpp .

Yet, there are a couple of libraries with higher-level APIs. Take https://wiki.freebsd.org/LibElf as a starting point.

Upvotes: 0

Jeegar Patel
Jeegar Patel

Reputation: 27210

Open your elf file using fopen()

FILE *fp  = fopen("youe_elf.bin","rb");

Now using fread() Read block of data from file. and map with the format of ELF file and get header bytes and write them in new file again using fopen()

Upvotes: 0

Related Questions