61 lines
1.7 KiB
C
61 lines
1.7 KiB
C
/*
|
|
* CS 261 PA1: Mini-ELF header verifier
|
|
*
|
|
* Name: Nicholas Tamassia
|
|
*/
|
|
|
|
#include "p1-check.h"
|
|
|
|
/**********************************************************************
|
|
* REQUIRED FUNCTIONS
|
|
*********************************************************************/
|
|
|
|
bool read_header (FILE *file, elf_hdr_t *hdr)
|
|
{
|
|
if (!file || !hdr) {
|
|
return false;
|
|
}
|
|
|
|
return ((fread(hdr, sizeof(elf_hdr_t), 1, file) == 1) && hdr->magic == 0x00464c45);
|
|
}
|
|
|
|
/**********************************************************************
|
|
* OPTIONAL FUNCTIONS
|
|
*********************************************************************/
|
|
|
|
void dump_header (elf_hdr_t *hdr)
|
|
{
|
|
uint8_t *byte_arr = (uint8_t*)hdr;
|
|
size_t size = sizeof(elf_hdr_t) / sizeof(byte_arr[0]);
|
|
|
|
for (int i = 0; i < size; i++) {
|
|
printf("%02x", byte_arr[i]);
|
|
|
|
if (i != size - 1) {
|
|
printf(" ");
|
|
}
|
|
|
|
if (i == size / 2 - 1) {
|
|
printf(" ");
|
|
}
|
|
}
|
|
printf("\n");
|
|
|
|
printf("Mini-ELF version %d\n", hdr->e_version);
|
|
printf("Entry point 0x%03x\n", hdr->e_entry);
|
|
printf("There are %d program headers, starting at offset %d (0x%02x)\n", hdr->e_num_phdr, hdr->e_phdr_start, hdr->e_phdr_start);
|
|
|
|
if (hdr->e_symtab != 0) {
|
|
printf("There is a symbol table starting at offset %d (0x%02x)\n", hdr->e_symtab, hdr->e_symtab);
|
|
} else {
|
|
printf("There is no symbol table present\n");
|
|
}
|
|
|
|
if (hdr->e_strtab != 0) {
|
|
printf("There is a string table starting at offset %d (0x%02x)\n", hdr->e_strtab, hdr->e_strtab);
|
|
} else {
|
|
printf("There is no string table present\n");
|
|
}
|
|
}
|
|
|