[util] Readelf utility, actually reading some elfs. Cool.
This commit is contained in:
parent
60016e6e2b
commit
d31d763058
@ -1,10 +1,11 @@
|
||||
/*
|
||||
/**
|
||||
* ELF Binary Executable headers
|
||||
*
|
||||
* vim:noexpandtab
|
||||
* vim:tabstop=4
|
||||
*/
|
||||
|
||||
#ifndef _ELF_H
|
||||
#define _ELF_H
|
||||
|
||||
/*
|
||||
* Different bits of our build environment
|
||||
* require different header files for definitions
|
||||
@ -19,4 +20,119 @@
|
||||
# endif
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Unless otherwise stated, the definitions herein
|
||||
* are sourced from the Portable Formats Specification,
|
||||
* version 1.1 - ELF: Executable and Linkable Format
|
||||
*/
|
||||
|
||||
/*
|
||||
* ELF Magic Signature
|
||||
*/
|
||||
#define ELFMAG0 0x7f
|
||||
#define ELFMAG1 'E'
|
||||
#define ELFMAG2 'L'
|
||||
#define ELFMAG3 'F'
|
||||
#define EI_NIDENT 16
|
||||
|
||||
/*
|
||||
* ELF Datatypes
|
||||
*/
|
||||
typedef uint32_t Elf32_Word;
|
||||
typedef uint32_t Elf32_Addr;
|
||||
typedef uint32_t Elf32_Off;
|
||||
typedef uint32_t Elf32_Sword;
|
||||
typedef uint16_t Elf32_Half;
|
||||
|
||||
/*
|
||||
* ELF Header
|
||||
*/
|
||||
typedef struct {
|
||||
unsigned char e_ident[EI_NIDENT];
|
||||
Elf32_Half e_type;
|
||||
Elf32_Half e_machine;
|
||||
Elf32_Word e_version;
|
||||
Elf32_Addr e_entry;
|
||||
Elf32_Off e_phoff;
|
||||
Elf32_Off e_shoff;
|
||||
Elf32_Word e_flags;
|
||||
Elf32_Half e_ehsize;
|
||||
Elf32_Half e_phentsize;
|
||||
Elf32_Half e_phnum;
|
||||
Elf32_Half e_shentsize;
|
||||
Elf32_Half e_shnum;
|
||||
Elf32_Half e_shstrndx;
|
||||
} Elf32_Header;
|
||||
|
||||
/*
|
||||
* e_type
|
||||
*/
|
||||
|
||||
#define ET_NONE 0 /* No file type */
|
||||
#define ET_REL 1 /* Relocatable file */
|
||||
#define ET_EXEC 2 /* Executable file */
|
||||
#define ET_DYN 3 /* Shared object file */
|
||||
#define ET_CORE 4 /* Core file */
|
||||
#define ET_LOPROC 0xff0 /* [Processor Specific] */
|
||||
#define ET_HIPROC 0xfff /* [Processor Specific] */
|
||||
|
||||
/*
|
||||
* Machine types
|
||||
*/
|
||||
#define EM_NONE 0
|
||||
#define EM_386 3
|
||||
|
||||
#define EV_NONE 0
|
||||
#define EV_CURRENT 1
|
||||
|
||||
/** Program Header */
|
||||
typedef struct {
|
||||
Elf32_Word p_type;
|
||||
Elf32_Off p_offset;
|
||||
Elf32_Addr p_vaddr;
|
||||
Elf32_Addr p_paddr;
|
||||
Elf32_Word p_filesz;
|
||||
Elf32_Word p_flags;
|
||||
Elf32_Word p_align;
|
||||
} Elf32_Phdr;
|
||||
|
||||
/* p_type values */
|
||||
#define PT_NULL 0 /* Unused, skip me */
|
||||
#define PT_LOAD 1 /* Loadable segment */
|
||||
#define PT_DYNAMIC 2 /* Dynamic linking information */
|
||||
#define PT_INTERP 3 /* Interpreter (null-terminated string, pathname) */
|
||||
#define PT_NOTE 4 /* Auxillary information */
|
||||
#define PT_SHLIB 5 /* Reserved. */
|
||||
#define PT_PHDR 6 /* Oh, it's me. Hello! Back-reference to the header table itself */
|
||||
#define PT_LOPROC 0x70000000
|
||||
#define PT_HIPROC 0x7FFFFFFF
|
||||
|
||||
|
||||
/** Section Header */
|
||||
typedef struct {
|
||||
Elf32_Word sh_name;
|
||||
Elf32_Word sh_type;
|
||||
Elf32_Word sh_flags;
|
||||
Elf32_Addr sh_addr;
|
||||
Elf32_Off sh_offset;
|
||||
Elf32_Word sh_size;
|
||||
Elf32_Word sh_link;
|
||||
Elf32_Word sh_info;
|
||||
Elf32_Word sh_addralign;
|
||||
Elf32_Word sh_entsize;
|
||||
} Elf32_Shdr;
|
||||
|
||||
/* sh_type values */
|
||||
#define SHT_NONE 0
|
||||
#define SHT_PROGBITS 1
|
||||
#define SHT_SYMTAB 2
|
||||
#define SHT_STRTAB 3
|
||||
|
||||
|
||||
#endif /* _ELF_H*/
|
||||
|
||||
|
||||
/*
|
||||
* vim:noexpandtab
|
||||
* vim:tabstop=4
|
||||
*/
|
||||
|
189
util/readelf.c
189
util/readelf.c
@ -1,13 +1,192 @@
|
||||
/*
|
||||
* ToAruOS Miniature ELF Reader
|
||||
*
|
||||
* vim:noexpandtab
|
||||
* vim:tabstop=4
|
||||
* (C) 2011 Kevin Lange
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
|
||||
/* The Master ELF Header */
|
||||
#include "../kernel/include/elf.h"
|
||||
|
||||
int main(int argc, char ** argv) {
|
||||
return -1;
|
||||
/**
|
||||
* Show usage for the readelf application.
|
||||
* @param argc Argument count (unused)
|
||||
* @param argv Arguments to binary
|
||||
*/
|
||||
void usage(int argc, char ** argv) {
|
||||
/* Show usage */
|
||||
printf("%s [filename]\n", argv[0]);
|
||||
printf("\tDisplays information on ELF binaries such as section names,\n");
|
||||
printf("\tlocations, sizes, and loading positions in memory.\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Application entry point.
|
||||
* @returns 0 on sucess, 1 on failure
|
||||
*/
|
||||
int main(int argc, char ** argv) {
|
||||
/* Process arguments */
|
||||
if (argc < 2) usage(argc,argv);
|
||||
|
||||
FILE * binary; /**< File pointer for requested executable */
|
||||
size_t binary_size; /**< Size of the file */
|
||||
char * binary_buf; /**< Buffer to store the binary in memory */
|
||||
Elf32_Header * header; /**< ELF header */
|
||||
char * string_table[5]; /**< Room for some string tables */
|
||||
|
||||
/* Open the requested binary */
|
||||
binary = fopen(argv[1], "r");
|
||||
|
||||
/* Jump to the end so we can get the size */
|
||||
fseek(binary, 0, SEEK_END);
|
||||
binary_size = ftell(binary);
|
||||
fseek(binary, 0, SEEK_SET);
|
||||
|
||||
/* Some sanity checks */
|
||||
if (binary_size < 4 || binary_size > 0xFFFFFFF) {
|
||||
printf("Oh no! I don't quite like the size of this binary.\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Read the binary into a buffer */
|
||||
binary_buf = malloc(binary_size);
|
||||
fread((void *)binary_buf, binary_size, 1, binary);
|
||||
|
||||
/* Let's start considering this guy an elf, 'eh? */
|
||||
header = (Elf32_Header *)binary_buf;
|
||||
|
||||
/* Verify the magic */
|
||||
if ( header->e_ident[0] != ELFMAG0 ||
|
||||
header->e_ident[1] != ELFMAG1 ||
|
||||
header->e_ident[2] != ELFMAG2 ||
|
||||
header->e_ident[3] != ELFMAG3) {
|
||||
printf("Header magic is wrong!\n");
|
||||
}
|
||||
|
||||
/* Let's print out some of the header information, shall we? */
|
||||
printf("\033[1mELF Header\033[0m\n");
|
||||
|
||||
/* File type */
|
||||
printf("[Type %d] ", header->e_type);
|
||||
switch (header->e_type) {
|
||||
case ET_NONE:
|
||||
printf("No file type.\n");
|
||||
break;
|
||||
case ET_REL:
|
||||
printf("Relocatable file.\n");
|
||||
break;
|
||||
case ET_EXEC:
|
||||
printf("Executable file.\n");
|
||||
break;
|
||||
case ET_DYN:
|
||||
printf("Shared object file.\n");
|
||||
break;
|
||||
case ET_CORE:
|
||||
printf("Core file.\n");
|
||||
break;
|
||||
default:
|
||||
printf("(Unknown file type)\n");
|
||||
break;
|
||||
}
|
||||
|
||||
/* Machine Type */
|
||||
switch (header->e_machine) {
|
||||
case EM_386:
|
||||
printf("Intel x86\n");
|
||||
break;
|
||||
default:
|
||||
printf("Unknown machine: %d\n", header->e_machine);
|
||||
break;
|
||||
}
|
||||
|
||||
/* Version == EV_CURRENT? */
|
||||
if (header->e_version == EV_CURRENT) {
|
||||
printf("ELF version is 1, as it should be.\n");
|
||||
}
|
||||
|
||||
/* Entry point in memory */
|
||||
printf("Binary entry point in virtual memory is at 0x%x\n", header->e_entry);
|
||||
|
||||
/* Program header table offset */
|
||||
printf("Program header table is at +0x%x and one entry is 0x%x bytes.\n"
|
||||
"There are %d total program headers.\n",
|
||||
header->e_phoff, header->e_phentsize, header->e_phnum);
|
||||
|
||||
/* Section header table offset */
|
||||
printf("Section header table is at +0x%x and one entry is 0x%x bytes.\n"
|
||||
"There are %d total section headers.\n",
|
||||
header->e_shoff, header->e_shentsize, header->e_shnum);
|
||||
|
||||
/* Read the program headers */
|
||||
printf("\033[1mProgram Headers\033[0m\n");
|
||||
for (uint32_t x = 0; x < header->e_phentsize * header->e_phnum; x += header->e_phentsize) {
|
||||
if (header->e_phoff + x > binary_size) {
|
||||
printf("Tried to read beyond the end of the file.\n");
|
||||
return 1;
|
||||
}
|
||||
/* Grab the program header */
|
||||
Elf32_Phdr * phdr = (Elf32_Phdr *)((uintptr_t)binary_buf + (header->e_phoff + x));
|
||||
|
||||
/* Print the header type */
|
||||
switch (phdr->p_type) {
|
||||
case PT_LOAD:
|
||||
printf("[Loadable Segment]\n");
|
||||
break;
|
||||
case PT_DYNAMIC:
|
||||
printf("[Dynamic Loading Information]\n");
|
||||
break;
|
||||
case PT_INTERP:
|
||||
printf("[Interpreter Path]\n");
|
||||
break;
|
||||
default:
|
||||
printf("[Unused Segement]\n");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
printf("\033[1mString Tables\033[0m\n");
|
||||
uint32_t i = 0;
|
||||
for (uint32_t x = 0; x < header->e_shentsize * header->e_shnum; x += header->e_shentsize) {
|
||||
if (header->e_shoff + x > binary_size) {
|
||||
printf("Tried to read beyond the end of the file.\n");
|
||||
return 1;
|
||||
}
|
||||
Elf32_Shdr * shdr = (Elf32_Shdr *)((uintptr_t)binary_buf + (header->e_shoff + x));
|
||||
if (shdr->sh_type != SHT_STRTAB) continue;
|
||||
string_table[i] = (char *)((uintptr_t)binary_buf + shdr->sh_offset);
|
||||
printf("Found a string table at 0x%x\n", shdr->sh_offset);
|
||||
++i;
|
||||
if (i == 5) break;
|
||||
}
|
||||
|
||||
printf("\033[1mSection Headers\033[0m\n");
|
||||
for (uint32_t x = 0; x < header->e_shentsize * header->e_shnum; x += header->e_shentsize) {
|
||||
if (header->e_shoff + x > binary_size) {
|
||||
printf("Tried to read beyond the end of the file.\n");
|
||||
return 1;
|
||||
}
|
||||
Elf32_Shdr * shdr = (Elf32_Shdr *)((uintptr_t)binary_buf + (header->e_shoff + x));
|
||||
|
||||
printf("[%d] %s\n", shdr->sh_type, (char *)((uintptr_t)string_table[0] + shdr->sh_name));
|
||||
printf("Section starts at 0x%x and is 0x%x bytes long.\n", shdr->sh_offset, shdr->sh_size);
|
||||
if (shdr->sh_addr) {
|
||||
printf("It should be loaded at 0x%x.\n", shdr->sh_addr);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* Read the section headers */
|
||||
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* vim:noexpandtab
|
||||
* vim:tabstop=4
|
||||
* vim:shiftwidth=4
|
||||
*/
|
||||
|
Loading…
x
Reference in New Issue
Block a user