diff --git a/build.sh b/build.sh index 3b887e5..3d9214d 100755 --- a/build.sh +++ b/build.sh @@ -1,2 +1,2 @@ -gcc -o main.o elf_header.c main.c elf_osabi.c -g -Wall -DDEBUG -Wpedantic -Wextra -Wunused-result -std=c99 -pedantic-errors +gcc -o main.o elf_header.c main.c elf_osabi.c elf_machine_type.c -g -Wall -DDEBUG -Wpedantic -Wextra -Wunused-result -std=c99 -pedantic-errors ./main.o "$1" diff --git a/elf_header.c b/elf_header.c index b687286..a854a03 100644 --- a/elf_header.c +++ b/elf_header.c @@ -1,5 +1,6 @@ #include "elf_header.h" #include "elf_osabi.h" +#include "elf_machine_type.h" #include #include @@ -19,8 +20,8 @@ typedef enum EIdent EI_CLASS = 4, EI_DATA = 5, EI_HEADER_VERSION = 6, - EI_OSABI = 7, - EI_ABIVERSION = 8, + EI_OSABI = ELF_OSABI_IDENT_INDEX, + EI_ABIVERSION = ELF_OSABI_VERSION_IDENT_INDEX, EI_PAD = 9, EI_NIDENT = ELF_IDENTITIFCATION_MAX_SIZE - 1, //End of reserved 16 bytes for the header. EI_TYPE = EI_NIDENT + 1, @@ -87,12 +88,17 @@ struct elf_header* ParseObjectFile(const char* bytes, size_t count) { if (bytes[EI_HEADER_VERSION] == 1) printf("ELF File Version: Current (1)\n"); else printf("ELF File Version: Invalid (%d)\n", bytes[EI_HEADER_VERSION]); - char buffer[255]; + char buffer[256]; GetELFOSABIString(bytes[EI_OSABI], buffer); printf("OS ABI: %s\n", buffer); + GetELFMachineTypeText(bytes[EI_MACHINE], buffer); + + printf("Machine type: %s\n", buffer); + printf("Machine: %d\n", bytes[EI_MACHINE]); + // int64_t entryAddress; // memcpy(&entryAddress, &bytes[EI_ENTRY_ADDRESS_POINT], sizeof(int64_t)); diff --git a/elf_header.h b/elf_header.h index 2a0b900..8914146 100644 --- a/elf_header.h +++ b/elf_header.h @@ -21,17 +21,4 @@ typedef enum ELFType{ ET_HIProc = 0xffff //Specific } ELFType; -typedef enum ELFMachine -{ - EM_None = 0, - EM_M32 = 1, //AT&T WE 32100 - EM_Sparc = 2, - EM_386 = 3, - EM_68k = 4, - EM_88k = 5, - EM_860 = 7, - EM_MIPS = 8, - EM_X86_64 = 62 -} ELFMachine; - #endif \ No newline at end of file diff --git a/elf_machine_type.c b/elf_machine_type.c new file mode 100644 index 0000000..14dc2da --- /dev/null +++ b/elf_machine_type.c @@ -0,0 +1,32 @@ +#include "elf_machine_type.h" +#include +#include +#include + +char* ELF_MACHINE_TYPE_TEXT[] = { + "No ISA Set", + "\x00\x01""AT&T WE 32100", + "\x00\x3E""Advanced Micro Devices x86_64" +}; + +void GetELFMachineTypeText(ELFMachine type, char buffer[256]) { + if (!buffer) return; + + memset(buffer, '\0', 256); + + if (type == 0) { + strncpy(buffer, ELF_MACHINE_TYPE_TEXT[0], strlen(ELF_MACHINE_TYPE_TEXT[0])); + + return; + } + + for(unsigned int i = 1; i < sizeof(ELF_MACHINE_TYPE_TEXT) / sizeof(char*); i++) { + char* text = ELF_MACHINE_TYPE_TEXT[i]; + + if (type == (unsigned int) text[0] + text[1]) { + strncpy(buffer, &text[2], strlen(&text[2])); + + return; + } + } +} \ No newline at end of file diff --git a/elf_machine_type.h b/elf_machine_type.h new file mode 100644 index 0000000..dc89055 --- /dev/null +++ b/elf_machine_type.h @@ -0,0 +1,19 @@ +#ifndef ELF_MACHINE_TYPES_H +#define ELF_MACHINE_TYPES_H + +typedef enum ELFMachine +{ + EM_None = 0, + EM_M32 = 1, //AT&T WE 32100 + EM_Sparc = 2, + EM_386 = 3, + EM_68k = 4, + EM_88k = 5, + EM_860 = 7, + EM_MIPS = 8, + EM_X86_64 = 0x3E +} ELFMachine; + +void GetELFMachineTypeText(ELFMachine type, char buffer[256]); + +#endif \ No newline at end of file diff --git a/elf_osabi.c b/elf_osabi.c index c24c49c..941b1a4 100644 --- a/elf_osabi.c +++ b/elf_osabi.c @@ -1,7 +1,7 @@ #include "elf_osabi.h" #include -char* ELFOSABISText[19] = +char* ELFOSABISText[] = { "System V ABI", "Hewlett-Packard HP-UX ABI", diff --git a/elf_osabi.h b/elf_osabi.h index 1d3be44..7b2f8b9 100644 --- a/elf_osabi.h +++ b/elf_osabi.h @@ -1,6 +1,9 @@ #ifndef ELF_OSABI_H #define ELF_OSABI_H +#define ELF_OSABI_IDENT_INDEX 0x07 +#define ELF_OSABI_VERSION_IDENT_INDEX 0x08 + typedef enum ELFOSABI { ELFOSABI_SYSTEMV = 0,