aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorStefan Bucur <stefanb@zytor.com>2008-06-06 17:52:47 +0300
committerStefan Bucur <stefanb@zytor.com>2008-06-06 17:52:47 +0300
commit7a6eedf7c031dd8abb33130af71441de5b914716 (patch)
tree8ce6cff9ff35072cacd85493c54e5a915970ae6e
parenta0fd2a522fbfb30573bb2e626453814e03eac36b (diff)
downloadsyslinux-elf-7a6eedf7c031dd8abb33130af71441de5b914716.tar.gz
syslinux-elf-7a6eedf7c031dd8abb33130af71441de5b914716.tar.xz
syslinux-elf-7a6eedf7c031dd8abb33130af71441de5b914716.zip
Implemented and tested ELF header structure.
-rw-r--r--elf/elf.h58
-rw-r--r--elf/elftest.c85
2 files changed, 143 insertions, 0 deletions
diff --git a/elf/elf.h b/elf/elf.h
index feadf661..eaa0f0c8 100644
--- a/elf/elf.h
+++ b/elf/elf.h
@@ -3,4 +3,62 @@
#ifndef ELF_H_
#define ELF_H_
+// ELF data types
+typedef unsigned long Elf32_Addr;
+typedef unsigned short Elf32_Half;
+typedef unsigned long Elf32_Off;
+typedef long Elf32_Sword;
+typedef unsigned long Elf32_Word;
+
+// Size of the magic section
+#define EI_NIDENT 16
+
+// Object file types
+enum e_type_enum {
+ ET_NONE = 0, // No type
+ ET_REL = 1, // Relocatable
+ ET_EXEC = 2, // Executable
+ ET_DYN = 3, // Shared object
+ ET_CORE = 4, // Core
+};
+
+// Machine types
+enum e_machine_enum {
+ EM_NONE = 0, // No machine
+ EM_M32 = 1, // AT&T WE 32100
+ EM_SPARC = 2, // SPARC
+ EM_386 = 3, // Intel 80386
+ EM_68K = 4, // Motorola 68000
+ EM_88K = 5, // Motorola 88000
+ EM_860 = 7, // Intel 80860
+ EM_MIPS = 8, // MIPS RS3000 Big Endian
+ EM_MIPS_RS4_BE = 10 // MIPS RS4000 Big Endian
+};
+
+// Object version
+enum e_version_enum {
+ EV_NONE = 0, // Invalid version
+ EV_CURRENT = 1 // Current version
+};
+
+// The ELF header
+typedef struct {
+ unsigned char e_ident[EI_NIDENT]; // Magic
+ Elf32_Half e_type; // Object file type
+ Elf32_Half e_machine; // Machine type
+ Elf32_Word e_version; // Object file version
+ Elf32_Addr e_entry; // Program entry point
+ Elf32_Off e_phoff; // Program Header Table (PHT) offset
+ Elf32_Off e_shoff; // Section Header Table (SHT) offset
+ Elf32_Word e_flags; // Processor specific flags
+ Elf32_Half e_ehsize; // ELF header size
+ Elf32_Half e_phentsize; // Size of an entry in PHT
+ Elf32_Half e_phnum; // Number of entries in PHT
+ Elf32_Half e_shentsize; // Size of an header in SHT
+ Elf32_Half e_shnum; // Number of entries in SHT
+ Elf32_Half shstrndx; // Section name string table index
+} Elf32_Ehdr;
+
+
+
#endif /*ELF_H_*/
diff --git a/elf/elftest.c b/elf/elftest.c
index 10ef2308..154708d6 100644
--- a/elf/elftest.c
+++ b/elf/elftest.c
@@ -1,5 +1,90 @@
+#include <stdio.h>
+#include <stdlib.h>
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/mman.h>
+#include <fcntl.h>
+#include <unistd.h>
+
#include "elf.h"
+void print_usage() {
+ fprintf(stderr, "Usage:\n");
+ fprintf(stderr, "\telftest objfile\n");
+}
+
+void print_elf_info(const char *file_name) {
+ int elf_fd = open(file_name, O_RDONLY);
+ void *elf_addr = NULL;
+ Elf32_Ehdr *elf_header;
+ struct stat elf_stat;
+ int i;
+
+ if (elf_fd < 0) {
+ perror("Could not open object file");
+ goto error;
+ }
+
+ if (fstat(elf_fd, &elf_stat) < 0) {
+ perror("Could not get file information");
+ goto error;
+ }
+
+ elf_addr = mmap(NULL, elf_stat.st_size, PROT_READ, MAP_PRIVATE, elf_fd, 0);
+
+ if (elf_addr == NULL) {
+ perror("Could not map the file into memory");
+ goto error;
+ }
+
+ elf_header = (Elf32_Ehdr*)elf_addr;
+
+ printf("Magic:\t\t");
+ for (i=0; i < EI_NIDENT; i++) {
+ printf("%d ", elf_header->e_ident[i]);
+ }
+ printf("\n");
+ printf("Type:\t\t%u\n", elf_header->e_type);
+ printf("Machine:\t%u\n", elf_header->e_machine);
+ printf("Version:\t%lu\n", elf_header->e_version);
+ printf("Entry:\t\t0x%08lx\n", elf_header->e_entry);
+ printf("PHT Offset:\t0x%08lx\n", elf_header->e_phoff);
+ printf("SHT Offset:\t0x%08lx\n", elf_header->e_shoff);
+ printf("Flags:\t\t%lu\n", elf_header->e_flags);
+ printf("Header size:\t%u (Structure size: %u)\n", elf_header->e_ehsize,
+ sizeof(Elf32_Ehdr));
+
+
+ munmap(elf_addr, elf_stat.st_size);
+ close(elf_fd);
+
+ return;
+
+error:
+ if (elf_addr != NULL)
+ munmap(elf_addr, elf_stat.st_size);
+
+ if (elf_fd >= 0)
+ close(elf_fd);
+ exit(2);
+}
+
int main(int argc, char **argv) {
+ const char *file_name = NULL;
+
+ // Skip program name
+ argc--;
+ argv++;
+
+ if (argc != 1) {
+ print_usage();
+ return 1;
+ }
+
+ file_name = argv[0];
+
+ print_elf_info(file_name);
+
return 0;
}