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;
+}
+
 static void print_capstone_detail(cs_insn *insn, char *buf, size_t len,
                                  struct annotate_args *args, u64 addr)
 {
 {
        struct annotation *notes = symbol__annotation(sym);
        struct map *map = args->ms.map;
-       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);
-       u64 len = end - start;
+       u64 len;
        u64 offset;
-       int i, fd, count;
+       int i, count;
        bool is_64bit = false;
        bool needs_cs_close = false;
        u8 *buf = NULL;
-       struct find_file_offset_data data = {
-               .ip = start,
-       };
        csh handle;
        cs_insn *insn;
        char disasm_buf[512];
        if (args->options->objdump_path)
                return -1;
 
-       nsinfo__mountns_enter(dso__nsinfo(dso), &nsc);
-       fd = open(filename, O_RDONLY);
-       nsinfo__mountns_exit(&nsc);
-       if (fd < 0)
-               return -1;
-
-       if (file__read_maps(fd, /*exe=*/true, find_file_offset, &data,
-                           &is_64bit) == 0)
-               goto err;
-
-       if (open_capstone_handle(args, is_64bit, &handle) < 0)
-               goto err;
-
-       needs_cs_close = true;
-
-       buf = malloc(len);
+       buf = read_symbol(filename, map, sym, &len, &is_64bit);
        if (buf == NULL)
-               goto err;
-
-       count = pread(fd, buf, len, data.offset);
-       close(fd);
-       fd = -1;
-
-       if ((u64)count != len)
-               goto err;
+               return -1;
 
        /* add the function address and name */
        scnprintf(disasm_buf, sizeof(disasm_buf), "%#"PRIx64" <%s>:",
 
        annotation_line__add(&dl->al, ¬es->src->source);
 
+       if (open_capstone_handle(args, is_64bit, &handle) < 0)
+               goto err;
+
+       needs_cs_close = true;
+
        count = cs_disasm(handle, buf, len, start, len, &insn);
        for (i = 0, offset = 0; i < count; i++) {
                int printed;
        return count < 0 ? count : 0;
 
 err:
-       if (fd >= 0)
-               close(fd);
        if (needs_cs_close) {
                struct disasm_line *tmp;