Print this page
3317 dis(1) should support cross-target disassembly


  10  * See the License for the specific language governing permissions
  11  * and limitations under the License.
  12  *
  13  * When distributing Covered Code, include this CDDL HEADER in each
  14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15  * If applicable, add the following below this CDDL HEADER, with the
  16  * fields enclosed by brackets "[]" replaced with your own identifying
  17  * information: Portions Copyright [yyyy] [name of copyright owner]
  18  *
  19  * CDDL HEADER END
  20  */
  21 
  22 /*
  23  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
  24  * Use is subject to license terms.
  25  */
  26 
  27 /*
  28  * Copyright 2007 Jason King.  All rights reserved.
  29  * Use is subject to license terms.

  30  */
  31 
  32 /*
  33  * The sparc disassembler is mostly straightforward, each instruction is
  34  * represented by an inst_t structure.  The inst_t definitions are organized
  35  * into tables.  The tables are correspond to the opcode maps documented in the
  36  * various sparc architecture manuals.  Each table defines the bit range of the
  37  * instruction whose value act as an index into the array of instructions.  A
  38  * table can also refer to another table if needed.  Each table also contains
  39  * a function pointer of type format_fcn that knows how to output the
  40  * instructions in the table, as well as handle any synthetic instructions
  41  *
  42  * Unfortunately, the changes from sparcv8 -> sparcv9 not only include new
  43  * instructions, they sometimes renamed or just reused the same instruction to
  44  * do different operations (i.e. the sparcv8 coprocessor instructions).  To
  45  * accommodate this, each table can define an overlay table.  The overlay table
  46  * is a list of (table index, architecture, new instruction definition) values.
  47  *
  48  *
  49  * Traversal starts with the first table,


  85  */
  86 
  87 #include <libdisasm.h>
  88 #include <stdlib.h>
  89 #include <stdio.h>
  90 #include <sys/types.h>
  91 #include <sys/byteorder.h>
  92 #include <string.h>
  93 
  94 #include "libdisasm_impl.h"
  95 #include "dis_sparc.h"
  96 
  97 static const inst_t *dis_get_overlay(dis_handle_t *, const table_t *,
  98     uint32_t);
  99 static uint32_t dis_get_bits(uint32_t, int, int);
 100 
 101 #if !defined(DIS_STANDALONE)
 102 static void do_binary(uint32_t);
 103 #endif /* DIS_STANDALONE */
 104 
 105 dis_handle_t *
 106 dis_handle_create(int flags, void *data, dis_lookup_f lookup_func,
 107     dis_read_f read_func)






 108 {

 109 
 110 #if !defined(DIS_STANDALONE)
 111         char *opt = NULL;
 112         char *opt2, *save, *end;
 113 #endif
 114         dis_handle_t *dhp;
 115 
 116         if ((flags & (DIS_SPARC_V8|DIS_SPARC_V9|DIS_SPARC_V9_SGI)) == 0) {


 117                 (void) dis_seterrno(E_DIS_INVALFLAG);
 118                 return (NULL);
 119         }
 120 
 121         if ((dhp = dis_zalloc(sizeof (struct dis_handle))) == NULL) {
 122                 (void) dis_seterrno(E_DIS_NOMEM);
 123                 return (NULL);
 124         }
 125 
 126         dhp->dh_lookup = lookup_func;
 127         dhp->dh_read = read_func;
 128         dhp->dh_flags = flags;
 129         dhp->dh_data = data;
 130         dhp->dh_debug = DIS_DEBUG_COMPAT;
 131 
 132 #if !defined(DIS_STANDALONE)
 133 
 134         opt = getenv("_LIBDISASM_DEBUG");
 135         if (opt == NULL)
 136                 return (dhp);
 137 
 138         opt2 = strdup(opt);
 139         if (opt2 == NULL) {
 140                 dis_handle_destroy(dhp);

 141                 (void) dis_seterrno(E_DIS_NOMEM);
 142                 return (NULL);
 143         }
 144         save = opt2;
 145 
 146         while (opt2 != NULL) {
 147                 end = strchr(opt2, ',');
 148 
 149                 if (end != 0)
 150                         *end++ = '\0';
 151 
 152                 if (strcasecmp("synth-all", opt2) == 0)
 153                         dhp->dh_debug |= DIS_DEBUG_SYN_ALL;
 154 
 155                 if (strcasecmp("compat", opt2) == 0)
 156                         dhp->dh_debug |= DIS_DEBUG_COMPAT;
 157 
 158                 if (strcasecmp("synth-none", opt2) == 0)
 159                         dhp->dh_debug &= ~(DIS_DEBUG_SYN_ALL|DIS_DEBUG_COMPAT);
 160 
 161                 if (strcasecmp("binary", opt2) == 0)
 162                         dhp->dh_debug |= DIS_DEBUG_PRTBIN;
 163 
 164                 if (strcasecmp("format", opt2) == 0)
 165                         dhp->dh_debug |= DIS_DEBUG_PRTFMT;
 166 
 167                 if (strcasecmp("all", opt2) == 0)
 168                         dhp->dh_debug = DIS_DEBUG_ALL;
 169 
 170                 if (strcasecmp("none", opt2) == 0)
 171                         dhp->dh_debug = DIS_DEBUG_NONE;
 172 
 173                 opt2 = end;
 174         }
 175         free(save);
 176 #endif /* DIS_STANDALONE */
 177         return (dhp);
 178 }
 179 
 180 void
 181 dis_handle_destroy(dis_handle_t *dhp)
 182 {
 183         dis_free(dhp, sizeof (dis_handle_t));
 184 }
 185 
 186 void
 187 dis_set_data(dis_handle_t *dhp, void *data)
 188 {
 189         dhp->dh_data = data;
 190 }
 191 
 192 void
 193 dis_flags_set(dis_handle_t *dhp, int f)
 194 {
 195         dhp->dh_flags |= f;
 196 }
 197 
 198 void
 199 dis_flags_clear(dis_handle_t *dhp, int f)

 200 {
 201         dhp->dh_flags &= ~f;
 202 }
 203 
 204 /* ARGSUSED */
 205 int
 206 dis_max_instrlen(dis_handle_t *dhp)
 207 {
 208         return (4);
 209 }
 210 
 211 /*
 212  * The dis_i386.c comment for this says it returns the previous instruction,
 213  * however, I'm fairly sure it's actually returning the _address_ of the
 214  * nth previous instruction.
 215  */
 216 /* ARGSUSED */
 217 uint64_t
 218 dis_previnstr(dis_handle_t *dhp, uint64_t pc, int n)
 219 {
 220         if (n <= 0)
 221                 return (pc);
 222 
 223         if (pc < n)
 224                 return (pc);
 225 
 226         return (pc - n*4);
 227 }
 228 
 229 /* ARGSUSED */
 230 int
 231 dis_instrlen(dis_handle_t *dhp, uint64_t pc)
 232 {
 233         return (4);
 234 }
 235 
 236 int
 237 dis_disassemble(dis_handle_t *dhp, uint64_t addr, char *buf, size_t buflen)

 238 {

 239         const table_t *tp = &initial_table;
 240         const inst_t *inp = NULL;
 241 
 242         uint32_t instr;
 243         uint32_t idx = 0;
 244 
 245         if (dhp->dh_read(dhp->dh_data, addr, &instr, sizeof (instr)) !=
 246             sizeof (instr))
 247                 return (-1);
 248 
 249         dhp->dh_buf    = buf;
 250         dhp->dh_buflen = buflen;
 251         dhp->dh_addr   = addr;
 252 
 253         buf[0] = '\0';
 254 
 255         /* this allows sparc code to be tested on x86 */

 256         instr = BE_32(instr);

 257 
 258 #if !defined(DIS_STANDALONE)
 259         if ((dhp->dh_debug & DIS_DEBUG_PRTBIN) != 0)
 260                 do_binary(instr);
 261 #endif /* DIS_STANDALONE */
 262 
 263         /* CONSTCOND */
 264         while (1) {
 265                 idx = dis_get_bits(instr, tp->tbl_field, tp->tbl_len);
 266                 inp = &tp->tbl_inp[idx];
 267 
 268                 inp = dis_get_overlay(dhp, tp, idx);
 269 
 270                 if ((inp->in_type == INST_NONE) ||
 271                     ((inp->in_arch & dhp->dh_flags) == 0))
 272                         goto error;
 273 
 274                 if (inp->in_type == INST_TBL) {
 275                         tp = inp->in_data.in_tbl;
 276                         continue;
 277                 }
 278 
 279                 break;


 324 
 325                 if ((tp->tbl_ovp[i].ov_inst.in_arch & dhp->dh_flags) == 0)
 326                         continue;
 327 
 328                 ip = &tp->tbl_ovp[i].ov_inst;
 329                 break;
 330         }
 331 
 332         return (ip);
 333 }
 334 
 335 #if !defined(DIS_STANDALONE)
 336 static void
 337 do_binary(uint32_t instr)
 338 {
 339         (void) fprintf(stderr, "DISASM: ");
 340         prt_binary(instr, 32);
 341         (void) fprintf(stderr, "\n");
 342 }
 343 #endif /* DIS_STANDALONE */

























  10  * See the License for the specific language governing permissions
  11  * and limitations under the License.
  12  *
  13  * When distributing Covered Code, include this CDDL HEADER in each
  14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15  * If applicable, add the following below this CDDL HEADER, with the
  16  * fields enclosed by brackets "[]" replaced with your own identifying
  17  * information: Portions Copyright [yyyy] [name of copyright owner]
  18  *
  19  * CDDL HEADER END
  20  */
  21 
  22 /*
  23  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
  24  * Use is subject to license terms.
  25  */
  26 
  27 /*
  28  * Copyright 2007 Jason King.  All rights reserved.
  29  * Use is subject to license terms.
  30  * Copyright 2012 Joshua M. Clulow <josh@sysmgr.org>
  31  */
  32 
  33 /*
  34  * The sparc disassembler is mostly straightforward, each instruction is
  35  * represented by an inst_t structure.  The inst_t definitions are organized
  36  * into tables.  The tables are correspond to the opcode maps documented in the
  37  * various sparc architecture manuals.  Each table defines the bit range of the
  38  * instruction whose value act as an index into the array of instructions.  A
  39  * table can also refer to another table if needed.  Each table also contains
  40  * a function pointer of type format_fcn that knows how to output the
  41  * instructions in the table, as well as handle any synthetic instructions
  42  *
  43  * Unfortunately, the changes from sparcv8 -> sparcv9 not only include new
  44  * instructions, they sometimes renamed or just reused the same instruction to
  45  * do different operations (i.e. the sparcv8 coprocessor instructions).  To
  46  * accommodate this, each table can define an overlay table.  The overlay table
  47  * is a list of (table index, architecture, new instruction definition) values.
  48  *
  49  *
  50  * Traversal starts with the first table,


  86  */
  87 
  88 #include <libdisasm.h>
  89 #include <stdlib.h>
  90 #include <stdio.h>
  91 #include <sys/types.h>
  92 #include <sys/byteorder.h>
  93 #include <string.h>
  94 
  95 #include "libdisasm_impl.h"
  96 #include "dis_sparc.h"
  97 
  98 static const inst_t *dis_get_overlay(dis_handle_t *, const table_t *,
  99     uint32_t);
 100 static uint32_t dis_get_bits(uint32_t, int, int);
 101 
 102 #if !defined(DIS_STANDALONE)
 103 static void do_binary(uint32_t);
 104 #endif /* DIS_STANDALONE */
 105 
 106 static void
 107 dis_sparc_handle_detach(dis_handle_t *dhp)
 108 {
 109         dis_free(dhp->dh_arch_private, sizeof (dis_handle_sparc_t));
 110         dhp->dh_arch_private = NULL;
 111 }
 112 
 113 static int
 114 dis_sparc_handle_attach(dis_handle_t *dhp)
 115 {
 116         dis_handle_sparc_t *dhx;
 117 
 118 #if !defined(DIS_STANDALONE)
 119         char *opt = NULL;
 120         char *opt2, *save, *end;
 121 #endif

 122 
 123         /* Validate architecture flags */
 124         if ((dhp->dh_flags & (DIS_SPARC_V8|DIS_SPARC_V9|DIS_SPARC_V9_SGI))
 125             == 0) {
 126                 (void) dis_seterrno(E_DIS_INVALFLAG);
 127                 return (-1);
 128         }
 129 
 130         if ((dhx = dis_zalloc(sizeof (dis_handle_sparc_t))) == NULL) {
 131                 (void) dis_seterrno(E_DIS_NOMEM);
 132                 return (NULL);
 133         }
 134         dhx->dhx_debug = DIS_DEBUG_COMPAT;
 135         dhp->dh_arch_private = dhx;




 136 
 137 #if !defined(DIS_STANDALONE)
 138 
 139         opt = getenv("_LIBDISASM_DEBUG");
 140         if (opt == NULL)
 141                 return (0);
 142 
 143         opt2 = strdup(opt);
 144         if (opt2 == NULL) {
 145                 dis_handle_destroy(dhp);
 146                 dis_free(dhx, sizeof (dis_handle_sparc_t));
 147                 (void) dis_seterrno(E_DIS_NOMEM);
 148                 return (-1);
 149         }
 150         save = opt2;
 151 
 152         while (opt2 != NULL) {
 153                 end = strchr(opt2, ',');
 154 
 155                 if (end != 0)
 156                         *end++ = '\0';
 157 
 158                 if (strcasecmp("synth-all", opt2) == 0)
 159                         dhx->dhx_debug |= DIS_DEBUG_SYN_ALL;
 160 
 161                 if (strcasecmp("compat", opt2) == 0)
 162                         dhx->dhx_debug |= DIS_DEBUG_COMPAT;
 163 
 164                 if (strcasecmp("synth-none", opt2) == 0)
 165                         dhx->dhx_debug &= ~(DIS_DEBUG_SYN_ALL|DIS_DEBUG_COMPAT);
 166 
 167                 if (strcasecmp("binary", opt2) == 0)
 168                         dhx->dhx_debug |= DIS_DEBUG_PRTBIN;
 169 
 170                 if (strcasecmp("format", opt2) == 0)
 171                         dhx->dhx_debug |= DIS_DEBUG_PRTFMT;
 172 
 173                 if (strcasecmp("all", opt2) == 0)
 174                         dhx->dhx_debug = DIS_DEBUG_ALL;
 175 
 176                 if (strcasecmp("none", opt2) == 0)
 177                         dhx->dhx_debug = DIS_DEBUG_NONE;
 178 
 179                 opt2 = end;
 180         }
 181         free(save);
 182 #endif /* DIS_STANDALONE */
 183         return (0);


















 184 }
 185 
 186 /* ARGSUSED */
 187 static int
 188 dis_sparc_max_instrlen(dis_handle_t *dhp)
 189 {
 190         return (4);
 191 }
 192 
 193 /* ARGSUSED */
 194 static int
 195 dis_sparc_min_instrlen(dis_handle_t *dhp)
 196 {
 197         return (4);
 198 }
 199 
 200 /*
 201  * The dis_i386.c comment for this says it returns the previous instruction,
 202  * however, I'm fairly sure it's actually returning the _address_ of the
 203  * nth previous instruction.
 204  */
 205 /* ARGSUSED */
 206 static uint64_t
 207 dis_sparc_previnstr(dis_handle_t *dhp, uint64_t pc, int n)
 208 {
 209         if (n <= 0)
 210                 return (pc);
 211 
 212         if (pc < n)
 213                 return (pc);
 214 
 215         return (pc - n*4);
 216 }
 217 
 218 /* ARGSUSED */
 219 static int
 220 dis_sparc_instrlen(dis_handle_t *dhp, uint64_t pc)
 221 {
 222         return (4);
 223 }
 224 
 225 static int
 226 dis_sparc_disassemble(dis_handle_t *dhp, uint64_t addr, char *buf,
 227     size_t buflen)
 228 {
 229         dis_handle_sparc_t *dhx = dhp->dh_arch_private;
 230         const table_t *tp = &initial_table;
 231         const inst_t *inp = NULL;
 232 
 233         uint32_t instr;
 234         uint32_t idx = 0;
 235 
 236         if (dhp->dh_read(dhp->dh_data, addr, &instr, sizeof (instr)) !=
 237             sizeof (instr))
 238                 return (-1);
 239 
 240         dhx->dhx_buf    = buf;
 241         dhx->dhx_buflen = buflen;
 242         dhp->dh_addr    = addr;
 243 
 244         buf[0] = '\0';
 245 
 246         /* this allows sparc code to be tested on x86 */
 247 #if !defined(DIS_STANDALONE)
 248         instr = BE_32(instr);
 249 #endif /* DIS_STANDALONE */
 250 
 251 #if !defined(DIS_STANDALONE)
 252         if ((dhx->dhx_debug & DIS_DEBUG_PRTBIN) != 0)
 253                 do_binary(instr);
 254 #endif /* DIS_STANDALONE */
 255 
 256         /* CONSTCOND */
 257         while (1) {
 258                 idx = dis_get_bits(instr, tp->tbl_field, tp->tbl_len);
 259                 inp = &tp->tbl_inp[idx];
 260 
 261                 inp = dis_get_overlay(dhp, tp, idx);
 262 
 263                 if ((inp->in_type == INST_NONE) ||
 264                     ((inp->in_arch & dhp->dh_flags) == 0))
 265                         goto error;
 266 
 267                 if (inp->in_type == INST_TBL) {
 268                         tp = inp->in_data.in_tbl;
 269                         continue;
 270                 }
 271 
 272                 break;


 317 
 318                 if ((tp->tbl_ovp[i].ov_inst.in_arch & dhp->dh_flags) == 0)
 319                         continue;
 320 
 321                 ip = &tp->tbl_ovp[i].ov_inst;
 322                 break;
 323         }
 324 
 325         return (ip);
 326 }
 327 
 328 #if !defined(DIS_STANDALONE)
 329 static void
 330 do_binary(uint32_t instr)
 331 {
 332         (void) fprintf(stderr, "DISASM: ");
 333         prt_binary(instr, 32);
 334         (void) fprintf(stderr, "\n");
 335 }
 336 #endif /* DIS_STANDALONE */
 337 
 338 static int
 339 dis_sparc_supports_flags(int flags)
 340 {
 341         int archflags = flags & DIS_ARCH_MASK;
 342 
 343         if (archflags == DIS_SPARC_V8 ||
 344             (archflags & (DIS_SPARC_V9 | DIS_SPARC_V8)) == DIS_SPARC_V9)
 345                 return (1);
 346 
 347         return (0);
 348 }
 349 
 350 const dis_arch_t dis_arch_sparc = {
 351         dis_sparc_supports_flags,
 352         dis_sparc_handle_attach,
 353         dis_sparc_handle_detach,
 354         dis_sparc_disassemble,
 355         dis_sparc_previnstr,
 356         dis_sparc_min_instrlen,
 357         dis_sparc_max_instrlen,
 358         dis_sparc_instrlen
 359 };