struct elf_file_t *elf_file_create_from_path(char *path) { struct elf_file_t *elf_file; struct stat st; FILE *f; void *buffer; int size; int count; /* Get file size */ if (stat(path, &st)) fatal("'%s': path not found", path); size = st.st_size; /* Open file */ f = fopen(path, "rt"); if (!f) fatal("'%s': cannot open file", path); /* Read file contents */ buffer = xmalloc(size); count = fread(buffer, 1, size, f); if (count != size) fatal("'%s': error reading file contents", path); /* Create ELF file */ elf_file = elf_file_create_from_allocated_buffer(buffer, size, path); return elf_file; }
struct elf_file_t *elf_file_create_from_buffer(void *ptr, int size, char *name) { struct elf_file_t *elf_file; void *ptr_copy; /* Make a copy of the buffer */ ptr_copy = xmalloc(size); memcpy(ptr_copy, ptr, size); /* Create ELF */ elf_file = elf_file_create_from_allocated_buffer(ptr_copy, size, name); return elf_file; }
struct elf_file_t *elf_file_create_from_buffer(void *ptr, int size, char *name) { struct elf_file_t *elf_file; void *ptr_copy; /* Make a copy of the buffer */ ptr_copy = malloc(size); if (!ptr_copy) fatal("%s: out of memory", __FUNCTION__); memcpy(ptr_copy, ptr, size); /* Create ELF */ elf_file = elf_file_create_from_allocated_buffer(ptr_copy, size, name); return elf_file; }