/* Given a buffer and buffer size, find out whether the buffer content is an external ELF or not. * Return 0 as Yes and -1 as No */ static int is_external_elf(void *ptr_buffer, int buf_size) { struct elf_file_t *elf_file; int i; struct elf_section_t *elf_section; elf_file = elf_file_create_from_buffer(ptr_buffer, buf_size, NULL); if (list_count(elf_file->section_list) != 5) { elf_file_free(elf_file); return -1; } else { for (i = 0; i < list_count(elf_file->section_list); i++) { elf_section = list_get(elf_file->section_list, i); if(!strcmp(elf_section->name,"binary")) { elf_file_free(elf_file); return 0; } } elf_file_free(elf_file); return -1; } return -1; }
void x86_loader_free(struct x86_loader_t *loader) { /* Check no more links */ assert(!loader->num_links); /* Free ELF file */ if (loader->elf_file) elf_file_free(loader->elf_file); /* Free arguments */ LINKED_LIST_FOR_EACH(loader->args) str_free(linked_list_get(loader->args)); linked_list_free(loader->args); /* Free environment variables */ LINKED_LIST_FOR_EACH(loader->env) str_free(linked_list_get(loader->env)); linked_list_free(loader->env); /* Free loader */ str_free(loader->interp); str_free(loader->exe); str_free(loader->cwd); str_free(loader->stdin_file); str_free(loader->stdout_file); free(loader); }
/* Free module */ void cuda_module_free(struct cuda_module_t *module) { assert(module->elf_file); elf_file_free(module->elf_file); free(module); }
/* Free module */ void cuda_module_free(CUmodule module) { list_remove(module_list, module); if (module->elf_file) elf_file_free(module->elf_file); free(module); }
void evg_opencl_program_free(struct evg_opencl_program_t *program) { /* Free lists */ list_free(program->constant_buffer_list); if (program->elf_file) elf_file_free(program->elf_file); evg_opencl_repo_remove_object(evg_emu->opencl_repo, program); free(program); }
void si_bin_file_free(struct si_bin_file_t *bin_file) { /* Free encoding dictionary */ while (list_count(bin_file->enc_dict)) free(list_remove_at(bin_file->enc_dict, 0)); list_free(bin_file->enc_dict); /* Free rest */ elf_file_free(bin_file->elf_file); free(bin_file); }
/* GPU disassembler tool */ void si_emu_disasm(char *path) { struct elf_file_t *elf_file; struct elf_symbol_t *symbol; struct elf_section_t *section; struct si_bin_file_t *amd_bin; char kernel_name[MAX_STRING_SIZE]; int i; /* Initialize disassembler */ si_disasm_init(); /* Decode external ELF */ elf_file = elf_file_create_from_path(path); for (i = 0; i < list_count(elf_file->symbol_table); i++) { /* Get symbol and section */ symbol = list_get(elf_file->symbol_table, i); section = list_get(elf_file->section_list, symbol->section); if (!section) continue; /* If symbol is '__OpenCL_XXX_kernel', it points to internal ELF */ if (str_prefix(symbol->name, "__OpenCL_") && str_suffix(symbol->name, "_kernel")) { /* Decode internal ELF */ str_substr(kernel_name, sizeof(kernel_name), symbol->name, 9, strlen(symbol->name) - 16); amd_bin = si_bin_file_create(section->buffer.ptr + symbol->value, symbol->size, kernel_name); /* Get kernel name */ printf("**\n** Disassembly for '__kernel %s'\n**\n\n", kernel_name); si_disasm_buffer(&amd_bin->enc_dict_entry_southern_islands->sec_text_buffer, stdout); printf("\n\n\n"); /* Free internal ELF */ si_bin_file_free(amd_bin); } } /* Free external ELF */ elf_file_free(elf_file); si_disasm_done(); /* End */ mhandle_done(); exit(0); }
void X86ContextLoadInterp(X86Context *self) { struct x86_loader_t *loader = self->loader; struct elf_file_t *elf_file; /* Open dynamic loader */ x86_loader_debug("\nLoading program interpreter '%s'\n", loader->interp); elf_file = elf_file_create_from_path(loader->interp); /* Load section from program interpreter */ X86ContextLoadELFSections(self, elf_file); /* Change program entry to the one specified by the interpreter */ loader->interp_prog_entry = elf_file->header->e_entry; x86_loader_debug(" program interpreter entry: 0x%x\n\n", loader->interp_prog_entry); elf_file_free(elf_file); }
static void internal_elf_file_free(struct elf_file_t *internal_elf_file) { elf_file_free(internal_elf_file); }
static void external_elf_file_free(struct elf_file_t *external_elf_file) { elf_file_free(external_elf_file); }
void mips_emu_disasm(char *path) { struct elf_file_t *elf_file; struct elf_section_t *section; struct elf_symbol_t *symbol, *print_symbol; char inst_str[MAX_STRING_SIZE]; int curr_sym; int i; void *inst_ptr; unsigned int print_symbol_address = 0; /* Initializations - build tables of istructions */ mips_asm_init(); /* Create an elf file from the executable */ elf_file = elf_file_create_from_path(path); /* Read Sections */ for (i=0; i < list_count(elf_file->section_list); i++) { /* Get section and skip it if it does not contain code */ section = list_get(elf_file->section_list, i); if (!(section->header->sh_flags & SHF_EXECINSTR)) continue; /* Title */ printf("\n\nDisassembly of section %s:", section->name); curr_sym = 0; symbol = list_get(elf_file->symbol_table, curr_sym); /* Decode and dump instructions */ for (inst_ptr = section->buffer.ptr; inst_ptr < section->buffer.ptr + section->buffer.size; inst_ptr += 4) { /* Symbol */ while (symbol && symbol->value < (section->header->sh_addr + section->buffer.pos)) { curr_sym++; symbol = list_get(elf_file->symbol_table, curr_sym); } if (symbol && symbol->value == (section->header->sh_addr + section->buffer.pos)) printf("\n\n%08x <%s>:", section->header->sh_addr + section->buffer.pos, symbol->name); mips_inst_hex_dump(stdout, inst_ptr, (section->header->sh_addr + section->buffer.pos)); mips_inst_dump(stdout, inst_str, MAX_STRING_SIZE, inst_ptr, section->buffer.pos, (section->header->sh_addr + section->buffer.pos), &print_symbol_address); if (print_symbol_address != 0) { print_symbol = elf_symbol_get_by_address(elf_file, print_symbol_address, 0); if (print_symbol->value == print_symbol_address) printf(" <%s>", print_symbol->name); else printf(" <%s+0x%x>", print_symbol->name, print_symbol_address - print_symbol->value); print_symbol_address = 0; } section->buffer.pos += 4; } } printf("\n"); elf_file_free(elf_file); mips_asm_done(); }