From c5f6f3db4a706c226ca3d2534157a4997a916248 Mon Sep 17 00:00:00 2001 From: David du Colombier <0intro@gmail.com> Date: Thu, 27 Jul 2017 23:32:52 +0200 Subject: [PATCH] add elftype function --- README.md | 1 + elf.h | 1 + print.c | 4 ++-- str.c | 23 +++++++++++++++++++++++ 4 files changed, 27 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 46a7b51..d071101 100644 --- a/README.md +++ b/README.md @@ -66,6 +66,7 @@ Functions int readelf(FILE *f, Fhdr *fp); uint8_t* readelfsection(FILE *f, char *name, uint64_t *size, Fhdr *fp); void freeelf(Fhdr *fp); +char* elftype(uint16_t type); ``` Example diff --git a/elf.h b/elf.h index f7a20ac..4bfb5b5 100644 --- a/elf.h +++ b/elf.h @@ -50,3 +50,4 @@ struct Fhdr { int readelf(FILE*, Fhdr*); uint8_t* readelfsection(FILE*, char*, uint64_t*, Fhdr*); void freeelf(Fhdr*); +char* elftype(uint16_t); diff --git a/print.c b/print.c index 582f9b2..01930b0 100644 --- a/print.c +++ b/print.c @@ -13,7 +13,7 @@ printelf32ehdr(Elf32_Ehdr *e, Fhdr *fp) printf("ident %.2x %c %c %c %.2x %.2x %.2x %.2x %.2x %.2x %.2x %.2x %.2x %.2x %.2x %.2x\n", e->ident[0], e->ident[1], e->ident[2], e->ident[3], e->ident[4], e->ident[5], e->ident[6], e->ident[7], e->ident[8], e->ident[9], e->ident[10], e->ident[11], e->ident[12], e->ident[13], e->ident[14], e->ident[15]); - printf("type %u\n", e->type); + printf("type %s (0x%.4x)\n", elftype(e->type), e->type); printf("machine %s (0x%.4x)\n", elfmachine(e->machine), e->machine); printf("version %u\n", e->version); printf("entry 0x%.8x\n", e->entry); @@ -36,7 +36,7 @@ printelf64ehdr(Elf64_Ehdr *e, Fhdr *fp) printf("ident %.2x %c %c %c %.2x %.2x %.2x %.2x %.2x %.2x %.2x %.2x %.2x %.2x %.2x %.2x\n", e->ident[0], e->ident[1], e->ident[2], e->ident[3], e->ident[4], e->ident[5], e->ident[6], e->ident[7], e->ident[8], e->ident[9], e->ident[10], e->ident[11], e->ident[12], e->ident[13], e->ident[14], e->ident[15]); - printf("type %u\n", e->type); + printf("type %s (0x%.4x)\n", elftype(e->type), e->type); printf("machine %s (0x%.4x)\n", elfmachine(e->machine), e->machine); printf("version %u\n", e->version); printf("entry 0x%.16" PRIx64 "\n", e->entry); diff --git a/str.c b/str.c index f27edf4..47e8fca 100644 --- a/str.c +++ b/str.c @@ -187,6 +187,29 @@ char *machinestr[] = { [EM_RISCV] = "RISC-V" }; +char *typestr[] = { + [ET_NONE] = "No file type", + [ET_REL] = "Relocatable file", + [ET_EXEC] = "Executable file", + [ET_DYN] = "Shared object file", + [ET_CORE] = "Core file", +}; + +char* +elftype(uint16_t type) +{ + if(type < nelem(typestr) && typestr[type]) + return typestr[type]; + + if (type >= ET_LOOS && type <= ET_HIOS) + return "Operating system-specific"; + + if (type >= ET_LOPROC) + return "Processor-specific"; + + return "Unknown type"; +} + char* elfmachine(uint16_t machine) {