Thomas Gleixner | 1ccea77 | 2019-05-19 15:51:43 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Josh Poimboeuf | 627fce1 | 2017-07-11 10:33:42 -0500 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2017 Josh Poimboeuf <jpoimboe@redhat.com> |
Josh Poimboeuf | 627fce1 | 2017-07-11 10:33:42 -0500 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <unistd.h> |
Julien Thierry | ee819ae | 2020-09-04 16:30:27 +0100 | [diff] [blame] | 7 | #include <linux/objtool.h> |
Matt Helsley | 0decf1f | 2020-05-19 13:55:33 -0700 | [diff] [blame] | 8 | #include <asm/orc_types.h> |
Vasily Gorbik | 7786032 | 2020-11-13 00:03:32 +0100 | [diff] [blame] | 9 | #include <objtool/objtool.h> |
| 10 | #include <objtool/warn.h> |
| 11 | #include <objtool/endianness.h> |
Josh Poimboeuf | 627fce1 | 2017-07-11 10:33:42 -0500 | [diff] [blame] | 12 | |
| 13 | static const char *reg_name(unsigned int reg) |
| 14 | { |
| 15 | switch (reg) { |
| 16 | case ORC_REG_PREV_SP: |
| 17 | return "prevsp"; |
| 18 | case ORC_REG_DX: |
| 19 | return "dx"; |
| 20 | case ORC_REG_DI: |
| 21 | return "di"; |
| 22 | case ORC_REG_BP: |
| 23 | return "bp"; |
| 24 | case ORC_REG_SP: |
| 25 | return "sp"; |
| 26 | case ORC_REG_R10: |
| 27 | return "r10"; |
| 28 | case ORC_REG_R13: |
| 29 | return "r13"; |
| 30 | case ORC_REG_BP_INDIRECT: |
| 31 | return "bp(ind)"; |
| 32 | case ORC_REG_SP_INDIRECT: |
| 33 | return "sp(ind)"; |
| 34 | default: |
| 35 | return "?"; |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | static const char *orc_type_name(unsigned int type) |
| 40 | { |
| 41 | switch (type) { |
Julien Thierry | ee819ae | 2020-09-04 16:30:27 +0100 | [diff] [blame] | 42 | case UNWIND_HINT_TYPE_CALL: |
Josh Poimboeuf | 627fce1 | 2017-07-11 10:33:42 -0500 | [diff] [blame] | 43 | return "call"; |
Julien Thierry | ee819ae | 2020-09-04 16:30:27 +0100 | [diff] [blame] | 44 | case UNWIND_HINT_TYPE_REGS: |
Josh Poimboeuf | 627fce1 | 2017-07-11 10:33:42 -0500 | [diff] [blame] | 45 | return "regs"; |
Julien Thierry | ee819ae | 2020-09-04 16:30:27 +0100 | [diff] [blame] | 46 | case UNWIND_HINT_TYPE_REGS_PARTIAL: |
| 47 | return "regs (partial)"; |
Josh Poimboeuf | 627fce1 | 2017-07-11 10:33:42 -0500 | [diff] [blame] | 48 | default: |
| 49 | return "?"; |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | static void print_reg(unsigned int reg, int offset) |
| 54 | { |
| 55 | if (reg == ORC_REG_BP_INDIRECT) |
| 56 | printf("(bp%+d)", offset); |
| 57 | else if (reg == ORC_REG_SP_INDIRECT) |
Peter Zijlstra | 87ccc82 | 2021-02-03 12:02:21 +0100 | [diff] [blame] | 58 | printf("(sp)%+d", offset); |
Josh Poimboeuf | 627fce1 | 2017-07-11 10:33:42 -0500 | [diff] [blame] | 59 | else if (reg == ORC_REG_UNDEFINED) |
| 60 | printf("(und)"); |
| 61 | else |
| 62 | printf("%s%+d", reg_name(reg), offset); |
| 63 | } |
| 64 | |
| 65 | int orc_dump(const char *_objname) |
| 66 | { |
| 67 | int fd, nr_entries, i, *orc_ip = NULL, orc_size = 0; |
| 68 | struct orc_entry *orc = NULL; |
| 69 | char *name; |
Mikulas Patocka | 14c47b54 | 2017-12-02 16:17:44 -0600 | [diff] [blame] | 70 | size_t nr_sections; |
| 71 | Elf64_Addr orc_ip_addr = 0; |
Josh Poimboeuf | 8782e7c | 2020-04-01 13:23:26 -0500 | [diff] [blame] | 72 | size_t shstrtab_idx, strtab_idx = 0; |
Josh Poimboeuf | 627fce1 | 2017-07-11 10:33:42 -0500 | [diff] [blame] | 73 | Elf *elf; |
| 74 | Elf_Scn *scn; |
| 75 | GElf_Shdr sh; |
| 76 | GElf_Rela rela; |
| 77 | GElf_Sym sym; |
| 78 | Elf_Data *data, *symtab = NULL, *rela_orc_ip = NULL; |
| 79 | |
| 80 | |
| 81 | objname = _objname; |
| 82 | |
| 83 | elf_version(EV_CURRENT); |
| 84 | |
| 85 | fd = open(objname, O_RDONLY); |
| 86 | if (fd == -1) { |
| 87 | perror("open"); |
| 88 | return -1; |
| 89 | } |
| 90 | |
| 91 | elf = elf_begin(fd, ELF_C_READ_MMAP, NULL); |
| 92 | if (!elf) { |
| 93 | WARN_ELF("elf_begin"); |
| 94 | return -1; |
| 95 | } |
| 96 | |
| 97 | if (elf_getshdrnum(elf, &nr_sections)) { |
| 98 | WARN_ELF("elf_getshdrnum"); |
| 99 | return -1; |
| 100 | } |
| 101 | |
| 102 | if (elf_getshdrstrndx(elf, &shstrtab_idx)) { |
| 103 | WARN_ELF("elf_getshdrstrndx"); |
| 104 | return -1; |
| 105 | } |
| 106 | |
| 107 | for (i = 0; i < nr_sections; i++) { |
| 108 | scn = elf_getscn(elf, i); |
| 109 | if (!scn) { |
| 110 | WARN_ELF("elf_getscn"); |
| 111 | return -1; |
| 112 | } |
| 113 | |
| 114 | if (!gelf_getshdr(scn, &sh)) { |
| 115 | WARN_ELF("gelf_getshdr"); |
| 116 | return -1; |
| 117 | } |
| 118 | |
| 119 | name = elf_strptr(elf, shstrtab_idx, sh.sh_name); |
| 120 | if (!name) { |
| 121 | WARN_ELF("elf_strptr"); |
| 122 | return -1; |
| 123 | } |
| 124 | |
| 125 | data = elf_getdata(scn, NULL); |
| 126 | if (!data) { |
| 127 | WARN_ELF("elf_getdata"); |
| 128 | return -1; |
| 129 | } |
| 130 | |
| 131 | if (!strcmp(name, ".symtab")) { |
| 132 | symtab = data; |
Josh Poimboeuf | 8782e7c | 2020-04-01 13:23:26 -0500 | [diff] [blame] | 133 | } else if (!strcmp(name, ".strtab")) { |
| 134 | strtab_idx = i; |
Josh Poimboeuf | 627fce1 | 2017-07-11 10:33:42 -0500 | [diff] [blame] | 135 | } else if (!strcmp(name, ".orc_unwind")) { |
| 136 | orc = data->d_buf; |
| 137 | orc_size = sh.sh_size; |
| 138 | } else if (!strcmp(name, ".orc_unwind_ip")) { |
| 139 | orc_ip = data->d_buf; |
| 140 | orc_ip_addr = sh.sh_addr; |
| 141 | } else if (!strcmp(name, ".rela.orc_unwind_ip")) { |
| 142 | rela_orc_ip = data; |
| 143 | } |
| 144 | } |
| 145 | |
Josh Poimboeuf | 8782e7c | 2020-04-01 13:23:26 -0500 | [diff] [blame] | 146 | if (!symtab || !strtab_idx || !orc || !orc_ip) |
Josh Poimboeuf | 627fce1 | 2017-07-11 10:33:42 -0500 | [diff] [blame] | 147 | return 0; |
| 148 | |
| 149 | if (orc_size % sizeof(*orc) != 0) { |
| 150 | WARN("bad .orc_unwind section size"); |
| 151 | return -1; |
| 152 | } |
| 153 | |
| 154 | nr_entries = orc_size / sizeof(*orc); |
| 155 | for (i = 0; i < nr_entries; i++) { |
| 156 | if (rela_orc_ip) { |
| 157 | if (!gelf_getrela(rela_orc_ip, i, &rela)) { |
| 158 | WARN_ELF("gelf_getrela"); |
| 159 | return -1; |
| 160 | } |
| 161 | |
| 162 | if (!gelf_getsym(symtab, GELF_R_SYM(rela.r_info), &sym)) { |
| 163 | WARN_ELF("gelf_getsym"); |
| 164 | return -1; |
| 165 | } |
| 166 | |
Josh Poimboeuf | 8782e7c | 2020-04-01 13:23:26 -0500 | [diff] [blame] | 167 | if (GELF_ST_TYPE(sym.st_info) == STT_SECTION) { |
| 168 | scn = elf_getscn(elf, sym.st_shndx); |
| 169 | if (!scn) { |
| 170 | WARN_ELF("elf_getscn"); |
| 171 | return -1; |
| 172 | } |
Josh Poimboeuf | 627fce1 | 2017-07-11 10:33:42 -0500 | [diff] [blame] | 173 | |
Josh Poimboeuf | 8782e7c | 2020-04-01 13:23:26 -0500 | [diff] [blame] | 174 | if (!gelf_getshdr(scn, &sh)) { |
| 175 | WARN_ELF("gelf_getshdr"); |
| 176 | return -1; |
| 177 | } |
Josh Poimboeuf | 627fce1 | 2017-07-11 10:33:42 -0500 | [diff] [blame] | 178 | |
Josh Poimboeuf | 8782e7c | 2020-04-01 13:23:26 -0500 | [diff] [blame] | 179 | name = elf_strptr(elf, shstrtab_idx, sh.sh_name); |
| 180 | if (!name) { |
| 181 | WARN_ELF("elf_strptr"); |
| 182 | return -1; |
| 183 | } |
| 184 | } else { |
| 185 | name = elf_strptr(elf, strtab_idx, sym.st_name); |
| 186 | if (!name) { |
| 187 | WARN_ELF("elf_strptr"); |
| 188 | return -1; |
| 189 | } |
Josh Poimboeuf | 627fce1 | 2017-07-11 10:33:42 -0500 | [diff] [blame] | 190 | } |
| 191 | |
Mikulas Patocka | 14c47b54 | 2017-12-02 16:17:44 -0600 | [diff] [blame] | 192 | printf("%s+%llx:", name, (unsigned long long)rela.r_addend); |
Josh Poimboeuf | 627fce1 | 2017-07-11 10:33:42 -0500 | [diff] [blame] | 193 | |
| 194 | } else { |
Mikulas Patocka | 14c47b54 | 2017-12-02 16:17:44 -0600 | [diff] [blame] | 195 | printf("%llx:", (unsigned long long)(orc_ip_addr + (i * sizeof(int)) + orc_ip[i])); |
Josh Poimboeuf | 627fce1 | 2017-07-11 10:33:42 -0500 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | |
| 199 | printf(" sp:"); |
| 200 | |
Vasily Gorbik | 8bfe273 | 2020-11-13 00:03:29 +0100 | [diff] [blame] | 201 | print_reg(orc[i].sp_reg, bswap_if_needed(orc[i].sp_offset)); |
Josh Poimboeuf | 627fce1 | 2017-07-11 10:33:42 -0500 | [diff] [blame] | 202 | |
| 203 | printf(" bp:"); |
| 204 | |
Vasily Gorbik | 8bfe273 | 2020-11-13 00:03:29 +0100 | [diff] [blame] | 205 | print_reg(orc[i].bp_reg, bswap_if_needed(orc[i].bp_offset)); |
Josh Poimboeuf | 627fce1 | 2017-07-11 10:33:42 -0500 | [diff] [blame] | 206 | |
Josh Poimboeuf | d31a580 | 2018-05-18 08:47:12 +0200 | [diff] [blame] | 207 | printf(" type:%s end:%d\n", |
| 208 | orc_type_name(orc[i].type), orc[i].end); |
Josh Poimboeuf | 627fce1 | 2017-07-11 10:33:42 -0500 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | elf_end(elf); |
| 212 | close(fd); |
| 213 | |
| 214 | return 0; |
| 215 | } |