Skip to content

Instantly share code, notes, and snippets.

@mepcotterell
Created January 24, 2019 20:39
Show Gist options
  • Save mepcotterell/8a048794bf7b8bd2122f56f38a318f1a to your computer and use it in GitHub Desktop.
Save mepcotterell/8a048794bf7b8bd2122f56f38a318f1a to your computer and use it in GitHub Desktop.

readelf.c

#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
#include <elf.h>

int main(int argc, char * argv []) {
  if (argc == 2) {

    char * filename = argv[1];
    int fd = open(filename, O_RDONLY);
    if (fd == -1) perror(argv[0]);

    Elf64_Ehdr elfh;
    int nread = read(fd, &elfh, sizeof(Elf64_Ehdr));
    if (nread == -1) perror(argv[0]);

    printf("nread = %d\n", nread);
    char m1 = (char) elfh.e_ident[1];
    char m2 = (char) elfh.e_ident[2];
    char m3 = (char) elfh.e_ident[3];
    printf("elf? = %c%c%c\n", m1, m2, m3);    
   
  } // if 
  return 0;
} // main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment