Skip to content

Instantly share code, notes, and snippets.

@halit
Created February 16, 2015 20:57
Show Gist options
  • Save halit/fb5009701c45850f9c3a to your computer and use it in GitHub Desktop.
Save halit/fb5009701c45850f9c3a to your computer and use it in GitHub Desktop.
Toy elf reader
#include <stdio.h>
#include <stdlib.h>
#include "elf.h"
void print_identifer(unsigned char e_ident[EI_NIDENT]){
printf("%-20s %x | %c%c%c\n", "Magic Header:", e_ident[EI_MAG0], e_ident[EI_MAG1], e_ident[EI_MAG2], e_ident[EI_MAG3]);
if(e_ident[EI_CLASS] == ELFCLASSNONE){
printf("%-20s %s", "Class type:", "Gecersiz sinif.");
}else if(e_ident[EI_CLASS] == ELFCLASS32){
printf("%-20s %s", "Class type:", "32 Bit sinif.");
}else if(e_ident[EI_CLASS] == ELFCLASS64){
printf("%-20s %s", "Class type:", "64 Bit sinif.");
}
printf("\n");
}
void print_header(Elf32_Ehdr* file_header){
print_identifer(file_header->e_ident);
}
int main(int argc, char *argv[]) {
if(argc < 2){
fprintf(stderr, "Usage: %s <elf_file>", argv[0]);
return 1;
}else{
FILE* fp = fopen(argv[1], "r");
if(fp == NULL){
fprintf(stderr, "No such file '%s'", argv[1]);
return 1;
}else{
Elf32_Ehdr* file_header = (Elf32_Ehdr*)calloc(sizeof(Elf32_Ehdr), 1);
if(file_header == NULL){
fprintf(stderr, "Memory allocation error.");
return 1;
}else{
fread(file_header, sizeof(Elf32_Ehdr), 1, fp);
print_header(file_header);
free(file_header);
}
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment