add elftype function

This commit is contained in:
David du Colombier 2017-07-27 23:32:52 +02:00
parent d0f39cd69b
commit c5f6f3db4a
4 changed files with 27 additions and 2 deletions

View File

@ -66,6 +66,7 @@ Functions
int readelf(FILE *f, Fhdr *fp); int readelf(FILE *f, Fhdr *fp);
uint8_t* readelfsection(FILE *f, char *name, uint64_t *size, Fhdr *fp); uint8_t* readelfsection(FILE *f, char *name, uint64_t *size, Fhdr *fp);
void freeelf(Fhdr *fp); void freeelf(Fhdr *fp);
char* elftype(uint16_t type);
``` ```
Example Example

1
elf.h
View File

@ -50,3 +50,4 @@ struct Fhdr {
int readelf(FILE*, Fhdr*); int readelf(FILE*, Fhdr*);
uint8_t* readelfsection(FILE*, char*, uint64_t*, Fhdr*); uint8_t* readelfsection(FILE*, char*, uint64_t*, Fhdr*);
void freeelf(Fhdr*); void freeelf(Fhdr*);
char* elftype(uint16_t);

View File

@ -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", 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[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]); 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("machine %s (0x%.4x)\n", elfmachine(e->machine), e->machine);
printf("version %u\n", e->version); printf("version %u\n", e->version);
printf("entry 0x%.8x\n", e->entry); 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", 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[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]); 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("machine %s (0x%.4x)\n", elfmachine(e->machine), e->machine);
printf("version %u\n", e->version); printf("version %u\n", e->version);
printf("entry 0x%.16" PRIx64 "\n", e->entry); printf("entry 0x%.16" PRIx64 "\n", e->entry);

23
str.c
View File

@ -187,6 +187,29 @@ char *machinestr[] = {
[EM_RISCV] = "RISC-V" [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* char*
elfmachine(uint16_t machine) elfmachine(uint16_t machine)
{ {