diff options
Diffstat (limited to 'tools/perf/util/llvm.c')
| -rw-r--r-- | tools/perf/util/llvm.c | 88 |
1 files changed, 15 insertions, 73 deletions
diff --git a/tools/perf/util/llvm.c b/tools/perf/util/llvm.c index b71649d616e3..ec41d0f27000 100644 --- a/tools/perf/util/llvm.c +++ b/tools/perf/util/llvm.c @@ -87,71 +87,6 @@ static void init_llvm(void) } } -struct find_file_offset_data { - u64 ip; - u64 offset; -}; - -/* This will be called for each PHDR in an ELF binary */ -static int find_file_offset(u64 start, u64 len, u64 pgoff, void *arg) -{ - struct find_file_offset_data *data = arg; - - if (start <= data->ip && data->ip < start + len) { - data->offset = pgoff + data->ip - start; - return 1; - } - return 0; -} - -static u8 * -read_symbol(const char *filename, struct map *map, struct symbol *sym, - u64 *len, bool *is_64bit) -{ - struct dso *dso = map__dso(map); - struct nscookie nsc; - u64 start = map__rip_2objdump(map, sym->start); - u64 end = map__rip_2objdump(map, sym->end); - int fd, count; - u8 *buf = NULL; - struct find_file_offset_data data = { - .ip = start, - }; - - *is_64bit = false; - - nsinfo__mountns_enter(dso__nsinfo(dso), &nsc); - fd = open(filename, O_RDONLY); - nsinfo__mountns_exit(&nsc); - if (fd < 0) - return NULL; - - if (file__read_maps(fd, /*exe=*/true, find_file_offset, &data, - is_64bit) == 0) - goto err; - - *len = end - start; - buf = malloc(*len); - if (buf == NULL) - goto err; - - count = pread(fd, buf, *len, data.offset); - close(fd); - fd = -1; - - if ((u64)count != *len) - goto err; - - return buf; - -err: - if (fd >= 0) - close(fd); - free(buf); - return NULL; -} -#endif - /* * Whenever LLVM wants to resolve an address into a symbol, it calls this * callback. We don't ever actually _return_ anything (in particular, because @@ -160,7 +95,6 @@ err: * should add some textual annotation for after the instruction. The caller * will use this information to add the actual annotation. */ -#ifdef HAVE_LIBLLVM_SUPPORT struct symbol_lookup_storage { u64 branch_addr; u64 pcrel_load_addr; @@ -191,8 +125,11 @@ int symbol__disassemble_llvm(const char *filename, struct symbol *sym, struct map *map = args->ms.map; struct dso *dso = map__dso(map); u64 start = map__rip_2objdump(map, sym->start); - u8 *buf; - u64 len; + /* Malloc-ed buffer containing instructions read from disk. */ + u8 *code_buf = NULL; + /* Pointer to code to be disassembled. */ + const u8 *buf; + u64 buf_len; u64 pc; bool is_64bit; char disasm_buf[2048]; @@ -207,7 +144,8 @@ int symbol__disassemble_llvm(const char *filename, struct symbol *sym, if (args->options->objdump_path) return -1; - buf = read_symbol(filename, map, sym, &len, &is_64bit); + buf = dso__read_symbol(dso, filename, map, sym, + &code_buf, &buf_len, &is_64bit); if (buf == NULL) return -1; @@ -259,14 +197,18 @@ int symbol__disassemble_llvm(const char *filename, struct symbol *sym, annotation_line__add(&dl->al, ¬es->src->source); pc = start; - for (u64 offset = 0; offset < len; ) { + for (u64 offset = 0; offset < buf_len; ) { unsigned int ins_len; storage.branch_addr = 0; storage.pcrel_load_addr = 0; - ins_len = LLVMDisasmInstruction(disasm, buf + offset, - len - offset, pc, + /* + * LLVM's API has the code be disassembled as non-const, cast + * here as we may be disassembling from mapped read-only memory. + */ + ins_len = LLVMDisasmInstruction(disasm, (u8 *)(buf + offset), + buf_len - offset, pc, disasm_buf, sizeof(disasm_buf)); if (ins_len == 0) goto err; @@ -324,7 +266,7 @@ int symbol__disassemble_llvm(const char *filename, struct symbol *sym, err: LLVMDisasmDispose(disasm); - free(buf); + free(code_buf); free(line_storage); return ret; #else // HAVE_LIBLLVM_SUPPORT |
