/* 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); }
/* Create a module */ struct cuda_module_t *cuda_module_create(char *cubin_path) { struct cuda_module_t *module; /* Initialize */ module = xcalloc(1, sizeof(struct cuda_module_t)); module->id = list_count(module_list); module->ref_count = 1; assert(cubin_path); module->elf_file = elf_file_create_from_path(cubin_path); list_add(module_list, module); return module; }
/* Create a module */ CUmodule cuda_module_create(const char *cubin_path) { CUmodule module; /* Create module */ module = (CUmodule)xcalloc(1, sizeof(struct CUmod_st)); /* Initialize */ module->id = list_count(module_list); module->elf_file = elf_file_create_from_path(cubin_path); list_add(module_list, module); return module; }
/* Create a module */ struct cuda_module_t *cuda_module_create(char *cubin_path) { struct cuda_module_t *module; /* Allocate */ module = xcalloc(1, sizeof(struct cuda_module_t)); /* Initialization */ module->id = list_count(module_list); module->elf_file = elf_file_create_from_path(cubin_path); /* Add module to list */ list_add(module_list, module); return module; }
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); }
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(); }
void X86ContextLoadExe(X86Context *self, char *exe) { struct x86_loader_t *loader = self->loader; struct mem_t *mem = self->mem; struct x86_file_desc_table_t *fdt = self->file_desc_table; char stdin_file_full_path[MAX_STRING_SIZE]; char stdout_file_full_path[MAX_STRING_SIZE]; char exe_full_path[MAX_STRING_SIZE]; /* Alternative stdin */ X86ContextGetFullPath(self, loader->stdin_file, stdin_file_full_path, MAX_STRING_SIZE); if (*stdin_file_full_path) { struct x86_file_desc_t *fd; fd = x86_file_desc_table_entry_get(fdt, 0); assert(fd); fd->host_fd = open(stdin_file_full_path, O_RDONLY); if (fd->host_fd < 0) fatal("%s: cannot open stdin", loader->stdin_file); x86_loader_debug("%s: stdin redirected\n", stdin_file_full_path); } /* Alternative stdout/stderr */ X86ContextGetFullPath(self, loader->stdout_file, stdout_file_full_path, MAX_STRING_SIZE); if (*stdout_file_full_path) { struct x86_file_desc_t *fd1, *fd2; fd1 = x86_file_desc_table_entry_get(fdt, 1); fd2 = x86_file_desc_table_entry_get(fdt, 2); assert(fd1 && fd2); fd1->host_fd = fd2->host_fd = open(stdout_file_full_path, O_CREAT | O_APPEND | O_TRUNC | O_WRONLY, 0660); if (fd1->host_fd < 0) fatal("%s: cannot open stdout/stderr", loader->stdout_file); x86_loader_debug("%s: stdout redirected\n", stdout_file_full_path); } /* Load program into memory */ X86ContextGetFullPath(self, exe, exe_full_path, MAX_STRING_SIZE); loader->elf_file = elf_file_create_from_path(exe_full_path); loader->exe = str_set(NULL, exe_full_path); /* Read sections and program entry */ X86ContextLoadELFSections(self, loader->elf_file); loader->prog_entry = loader->elf_file->header->e_entry; /* Set heap break to the highest written address rounded up to * the memory page boundary. */ mem->heap_break = ROUND_UP(mem->heap_break, MEM_PAGE_SIZE); /* Load program header table. If we found a PT_INTERP program header, * we have to load the program interpreter. This means we are dealing with * a dynamically linked application. */ X86ContextLoadProgramHeaders(self); if (loader->interp) X86ContextLoadInterp(self); /* Stack */ X86ContextLoadStack(self); /* Register initialization */ self->regs->eip = loader->interp ? loader->interp_prog_entry : loader->prog_entry; self->regs->esp = loader->environ_base; x86_loader_debug("Program entry is 0x%x\n", self->regs->eip); x86_loader_debug("Initial stack pointer is 0x%x\n", self->regs->esp); x86_loader_debug("Heap start set to 0x%x\n", mem->heap_break); }