aboutsummaryrefslogtreecommitdiffstats
path: root/elf/elftest.c
diff options
context:
space:
mode:
Diffstat (limited to 'elf/elftest.c')
-rw-r--r--elf/elftest.c88
1 files changed, 31 insertions, 57 deletions
diff --git a/elf/elftest.c b/elf/elftest.c
index 5436b40e..6462ad6a 100644
--- a/elf/elftest.c
+++ b/elf/elftest.c
@@ -7,84 +7,58 @@
#include <fcntl.h>
#include <unistd.h>
-#include <elf.h>
+#include "elf_module.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;
+int main(int argc, char **argv) {
+ int res;
+ struct elf_module *module;
+ const char *module_name = NULL;
- if (elf_fd < 0) {
- perror("Could not open object file");
- goto error;
+ // Skip program name
+ argc--;
+ argv++;
+
+ if (argc != 1) {
+ print_usage();
+ return 1;
}
- if (fstat(elf_fd, &elf_stat) < 0) {
- perror("Could not get file information");
- goto error;
+ module_name = argv[0];
+
+ res = modules_init();
+
+ if (res < 0) {
+ fprintf(stderr, "Could not initialize module subsystem\n");
+ exit(1);
}
- elf_addr = mmap(NULL, elf_stat.st_size, PROT_READ, MAP_PRIVATE, elf_fd, 0);
+ module = module_alloc(module_name);
- if (elf_addr == NULL) {
- perror("Could not map the file into memory");
+ if (module == NULL) {
+ fprintf(stderr, "Could not allocate the module\n");
goto error;
}
- elf_header = (Elf32_Ehdr*)elf_addr;
+ res = module_load(module);
- printf("Identification:\t");
- for (i=0; i < EI_NIDENT; i++) {
- printf("%d ", elf_header->e_ident[i]);
+ if (res < 0) {
+ fprintf(stderr, "Could not load the module\n");
+ goto error;
}
- printf("\n");
- printf("Type:\t\t%u\n", elf_header->e_type);
- printf("Machine:\t%u\n", elf_header->e_machine);
- printf("Version:\t%u\n", elf_header->e_version);
- printf("Entry:\t\t0x%08x\n", elf_header->e_entry);
- printf("PHT Offset:\t0x%08x\n", elf_header->e_phoff);
- printf("SHT Offset:\t0x%08x\n", elf_header->e_shoff);
- printf("Flags:\t\t%u\n", elf_header->e_flags);
- printf("Header size:\t%u (Structure size: %u)\n", elf_header->e_ehsize,
- sizeof(Elf32_Ehdr));
+ module_unload(module);
- munmap(elf_addr, elf_stat.st_size);
- close(elf_fd);
+ modules_term();
- return;
+ return 0;
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++;
+ modules_term();
- if (argc != 1) {
- print_usage();
- return 1;
- }
-
- file_name = argv[0];
-
- print_elf_info(file_name);
-
- return 0;
+ return 1;
}