title: How to print entire ELF file header with all its information?
body: <p>I have to print the ELF file header. All the data it contains, just like when running readelf -h hello.bin. The program is in C. This is what i have untill now:</p>
  <pre><code>  typedef struct elf64_hdr {
    unsigned char e_ident[EI_NIDENT]; /* ELF &quot;magic number&quot; */
    Elf64_Half e_type;
    Elf64_Half e_machine;
    Elf64_Word e_version;
    Elf64_Addr e_entry;       /* Entry point virtual address */
    Elf64_Off e_phoff;        /* Program header table file offset */
    Elf64_Off e_shoff;        /* Section header table file offset */
    Elf64_Word e_flags;
    Elf64_Half e_ehsize;
    Elf64_Half e_phentsize;
    Elf64_Half e_phnum;
    Elf64_Half e_shentsize;
    Elf64_Half e_shnum;
    Elf64_Half e_shstrndx;
  } Elf64_Ehdr;
  
  typedef struct elf64_shdr {
    Elf64_Word sh_name;       /* Section name, index in string tbl */
    Elf64_Word sh_type;       /* Type of section */
    Elf64_Xword sh_flags;     /* Miscellaneous section attributes */
    Elf64_Addr sh_addr;       /* Section virtual addr at execution */
    Elf64_Off sh_offset;      /* Section file offset */
    Elf64_Xword sh_size;      /* Size of section in bytes */
    Elf64_Word sh_link;       /* Index of another section */
    Elf64_Word sh_info;       /* Additional section information */
    Elf64_Xword sh_addralign; /* Section alignment */
    Elf64_Xword sh_entsize;   /* Entry size if section holds table */
  } Elf64_Shdr;
  </code></pre>
  <p>These are the structures.
  Here are the variables declared in main:</p>
  <pre><code>  FILE* ElfFile = NULL;
    char* SectNames = NULL;
    Elf64_Ehdr elfHdr;
    Elf64_Shdr sectHdr;
    uint32_t idx;
  </code></pre>
  <p>Here is the relevant part of the code for printing, that needs your help:</p>
  <pre><code>// read ELF header, first thing in the file
    fread(&amp;elfHdr, 1, sizeof(Elf64_Ehdr), ElfFile); 
    SectNames = malloc(sectHdr.sh_size); //variable for section names (like &quot;Magic&quot;, &quot;Data&quot; etc.)
    fseek(ElfFile, sectHdr.sh_offset, SEEK_SET); //going to the offset of the section
    fread(SectNames, 1, sectHdr.sh_size, ElfFile); //reading the size of section
    for(int i=0; i&lt;sectHdr.sh_size; i++)
    {
      char *name1 = &quot;&quot;;
      fseek(ElfFile, elfHdr.e_shoff + i*sizeof(sectHdr), SEEK_SET);
      fread(&amp;sectHdr, 1, sizeof(sectHdr), ElfFile);
      name1 = SectNames + sectHdr.sh_name;
      printf(&quot;%s \n&quot;, name1);
    }
  </code></pre>
  <p>The code compiles but does not print anything. I expected to print the strings like &quot;Magic&quot;, &quot;Data&quot;, &quot;Class&quot; etc...</p>